Class: Autobuild::CMake

Inherits:
Configurable show all
Defined in:
lib/autobuild/packages/cmake.rb

Overview

Handler class to build CMake-based packages

Direct Known Subclasses

Orogen

Constant Summary collapse

DOXYGEN_ACCEPTED_VARIABLES =
{
    '@CMAKE_SOURCE_DIR@' => ->(pkg) { pkg.srcdir },
    '@PROJECT_SOURCE_DIR@' => ->(pkg) { pkg.srcdir },
    '@CMAKE_BINARY_DIR@' => ->(pkg) { pkg.builddir },
    '@PROJECT_BINARY_DIR@' => ->(pkg) { pkg.builddir },
    '@PROJECT_NAME@' => ->(pkg) { pkg.name }
}.freeze
CMAKE_EQVS =
{
    'ON' => 'ON',
    'YES' => 'ON',
    'OFF' => 'OFF',
    'NO' => 'OFF'
}.freeze
@@delete_obsolete_files_in_prefix =
false
@@defines =
Hash.new

Class Attribute Summary collapse

Instance Attribute Summary collapse

Attributes inherited from Configurable

#source_tree_excludes

Attributes inherited from Package

#dependencies, #env, #failures, #importdir, #importer, #logdir, #name, #prefix, #srcdir, #statistics, #update, #updated, #utilities

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Configurable

#builddir, #builddir=, #buildstamp, #ensure_dependencies_installed, #prepare_for_forced_build, #prepare_for_rebuild

Methods inherited from Package

[], #add_env_op, #add_stat, #all_dependencies, #applied_post_install?, #apply_env, #apply_post_install, #checked_out?, clear, #depends_on, #depends_on?, #disable, #disable_doc, #disable_phases, #disabled?, #doc_dir, #doc_dir=, #doc_disabled, #doc_target_dir, #doc_target_dir=, #doc_task, each, #enable_doc, #env_add, #env_add_path, #env_add_prefix, #env_set, #env_source_after, #error, #failed?, #file, #find_in_path, #fingerprint, #full_env, #generates_doc?, #has_doc?, #import=, #import_invoked?, #imported?, #in_dir, #inspect, #install_doc, #install_invoked?, #installed?, #installstamp, #isolate_errors, #message, #method_missing, #parallel_build_level, #parallel_build_level=, #post_install, #prepare_for_forced_build, #prepare_for_rebuild, #process_formatting_string, #progress, #progress_done, #progress_start, #provides, #resolve_dependency_env, #resolved_env, #respond_to_missing?, #run, #source_tree, #task, #to_s, #update?, #updated?, #utility, #warn, #working_directory

Constructor Details

#initialize(options) ⇒ CMake

Returns a new instance of CMake.



123
124
125
126
127
128
129
# File 'lib/autobuild/packages/cmake.rb', line 123

def initialize(options)
    @defines = Hash.new
    super
    @test_args = self.class.test_args.dup
    @delete_obsolete_files_in_prefix = self.class.
        delete_obsolete_files_in_prefix?
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Autobuild::Package

Class Attribute Details

.always_use_doc_target=(value) ⇒ Object (writeonly)

Flag controlling whether autobuild should run doxygen itself or use the “doc” target generated by CMake

This is experimental and OFF by default. See CMake#run_doxygen for more details

See also CMake#always_use_doc_target= and CMake#always_use_doc_target? for a per-package control of that feature



184
185
186
# File 'lib/autobuild/packages/cmake.rb', line 184

def always_use_doc_target=(value)
  @always_use_doc_target = value
end

.full_reconfigures=(value) ⇒ Object (writeonly)

Sets the attribute full_reconfigures

Parameters:

  • value

    the value to set the attribute full_reconfigures to.



27
28
29
# File 'lib/autobuild/packages/cmake.rb', line 27

def full_reconfigures=(value)
  @full_reconfigures = value
end

.generatorObject

Global default for the CMake generator to use. If nil (the default), the -G option will not be given at all. Will work only if the generator creates makefiles

It can be overriden on a per-package basis with CMake.generator=



38
39
40
# File 'lib/autobuild/packages/cmake.rb', line 38

def generator
  @generator
end

.module_pathObject (readonly)

Returns the value of attribute module_path.



40
41
42
# File 'lib/autobuild/packages/cmake.rb', line 40

def module_path
  @module_path
end

.prefix_pathObject (readonly)

Returns the value of attribute prefix_path.



40
41
42
# File 'lib/autobuild/packages/cmake.rb', line 40

def prefix_path
  @prefix_path
end

Instance Attribute Details

#always_reconfigureObject

If true, always run cmake before make during the build



85
86
87
# File 'lib/autobuild/packages/cmake.rb', line 85

def always_reconfigure
  @always_reconfigure
end

#always_use_doc_targetObject (readonly)

Flag controlling whether autobuild should run doxygen itself or use the “doc” target generated by CMake

This is experimental and OFF by default. See CMake#run_doxygen for more details

See also CMake.always_use_doc_target= and CMake.always_use_doc_target? for a global control of that feature



208
209
210
# File 'lib/autobuild/packages/cmake.rb', line 208

def always_use_doc_target
  @always_use_doc_target
end

#definesObject (readonly)

a key => value association of defines for CMake



70
71
72
# File 'lib/autobuild/packages/cmake.rb', line 70

def defines
  @defines
end

#delete_obsolete_files_in_prefix=(value) ⇒ Object (writeonly)



137
138
139
# File 'lib/autobuild/packages/cmake.rb', line 137

def delete_obsolete_files_in_prefix=(value)
  @delete_obsolete_files_in_prefix = value
end

#full_reconfigures=(value) ⇒ Object (writeonly)

If true, we always remove the CMake cache before reconfiguring.

See #full_reconfigures? for more details



89
90
91
# File 'lib/autobuild/packages/cmake.rb', line 89

def full_reconfigures=(value)
  @full_reconfigures = value
end

#generatorObject

The CMake generator to use. You must choose one that generates Makefiles. If not set for this package explicitely, it is using the global value CMake.generator.



97
98
99
# File 'lib/autobuild/packages/cmake.rb', line 97

def generator
    @generator || CMake.generator
end

#show_make_messages=(value) ⇒ Object (writeonly)

Sets the attribute show_make_messages

Parameters:

  • value

    the value to set the attribute show_make_messages to.



487
488
489
# File 'lib/autobuild/packages/cmake.rb', line 487

def show_make_messages=(value)
  @show_make_messages = value
end

#test_argsObject (readonly)

Arguments to be passed to the ‘make’ test target



82
83
84
# File 'lib/autobuild/packages/cmake.rb', line 82

def test_args
  @test_args
end

Class Method Details

.always_use_doc_target?Boolean

Flag controlling whether autobuild should run doxygen itself or use the “doc” target generated by CMake

This is experimental and OFF by default. See CMake#run_doxygen for more details

See also CMake#always_use_doc_target= and CMake#always_use_doc_target? for a per-package control of that feature

Returns:

  • (Boolean)


194
195
196
# File 'lib/autobuild/packages/cmake.rb', line 194

def always_use_doc_target?
    @always_use_doc_target
end

.builddirObject



12
13
14
# File 'lib/autobuild/packages/cmake.rb', line 12

def builddir
    @builddir || Configurable.builddir
end

.builddir=(new) ⇒ Object



16
17
18
19
20
21
22
23
24
25
# File 'lib/autobuild/packages/cmake.rb', line 16

def builddir=(new)
    if Pathname.new(new).absolute?
        raise ConfigException, "absolute builddirs are not supported"
    end
    if new.nil? || new.empty?
        raise ConfigException, "builddir must be non-nil and non-empty"
    end

    @builddir = new
end

.define(name, value) ⇒ Object



145
146
147
148
149
150
151
152
153
154
# File 'lib/autobuild/packages/cmake.rb', line 145

def self.define(name, value)
    @@defines[name] =
        if value.respond_to?(:to_str)
            value.to_str
        elsif value
            'ON'
        else
            'OFF'
        end
end

.definesObject



141
142
143
# File 'lib/autobuild/packages/cmake.rb', line 141

def self.defines
    @@defines
end

.delete_obsolete_files_in_prefix=(flag) ⇒ Object



58
59
60
# File 'lib/autobuild/packages/cmake.rb', line 58

def delete_obsolete_files_in_prefix=(flag)
    @@delete_obsolete_files_in_prefix = flag
end

.delete_obsolete_files_in_prefix?Boolean

Whether files that are not within CMake’s install manifest but are present in the prefix should be deleted. Note that the contents of #log_dir are unaffected.

It is false by default. Set to true only if each package has its own prefix.

Returns:

  • (Boolean)


53
54
55
# File 'lib/autobuild/packages/cmake.rb', line 53

def delete_obsolete_files_in_prefix?
    @@delete_obsolete_files_in_prefix
end

.full_reconfigures?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/autobuild/packages/cmake.rb', line 29

def full_reconfigures?
    @full_reconfigures
end

.show_make_messages=(value) ⇒ Object



493
494
495
# File 'lib/autobuild/packages/cmake.rb', line 493

def self.show_make_messages=(value)
    @show_make_messages = value
end

.show_make_messages?Boolean

Returns:

  • (Boolean)


489
490
491
# File 'lib/autobuild/packages/cmake.rb', line 489

def self.show_make_messages?
    @show_make_messages
end

.test_argsObject

Arguments that will be passed to the ‘make’ test target



43
44
45
# File 'lib/autobuild/packages/cmake.rb', line 43

def test_args
    @test_args ||= ["-V"]
end

Instance Method Details

#all_definesObject

The list of all -D options that should be passed on to CMake



73
74
75
76
77
78
79
# File 'lib/autobuild/packages/cmake.rb', line 73

def all_defines
    additional_defines = Hash[
        "CMAKE_INSTALL_PREFIX" => prefix,
        "CMAKE_MODULE_PATH" => module_path.join(";"),
        "CMAKE_PREFIX_PATH" => prefix_path.join(";")]
    self.class.defines.merge(additional_defines).merge(defines)
end

#always_use_doc_target?Boolean

Flag controlling whether autobuild should run doxygen itself or use the “doc” target generated by CMake

This is experimental and OFF by default. See CMake#run_doxygen for more details

See also CMake.always_use_doc_target= and CMake.always_use_doc_target? for a global control of that feature

Returns:

  • (Boolean)


218
219
220
221
222
223
224
# File 'lib/autobuild/packages/cmake.rb', line 218

def always_use_doc_target?
    if @always_use_doc_target.nil?
        CMake.always_use_doc_target?
    else
        @always_use_doc_target
    end
end

#buildObject

Do the build in builddir



498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
# File 'lib/autobuild/packages/cmake.rb', line 498

def build
    current_message = +""
    in_dir(builddir) do
        progress_start "building %s" do
            if always_reconfigure || !File.file?('Makefile')
                run('build', Autobuild.tool(:cmake), '.')
            end

            warning_count = 0
            Autobuild.make_subcommand(self, 'build') do |line|
                needs_display = false
                if line =~ /\[\s*(\d+)%\]/
                    progress "building %s (#{Integer($1)}%)"
                elsif line !~ /^(?:Generating|Linking|Scanning|Building|Built)/
                    warning_count += 1 if line =~ /warning/
                    if show_make_messages?
                        current_message += line + "\n"
                        needs_display = true
                    end
                end
                if !needs_display && !current_message.empty?
                    current_message.split("\n").each do |l|
                        message "%s: #{l}", :magenta
                    end
                    current_message.clear
                end
            end
            current_message.split("\n").each do |l|
                message "%s: #{l}", :magenta
            end
            if warning_count > 0
                msg_warning = Autoproj.color("(#{warning_count} warnings)", :bold)
                progress_done "built %s #{msg_warning}"
            else
                progress_done "built %s"
            end
        end
    end
    Autobuild.touch_stamp(buildstamp)
rescue ::Exception
    current_message.split("\n").each do |l|
        message "%s: #{l}", :magenta
    end
    raise
end

#cmake_cacheObject



115
116
117
# File 'lib/autobuild/packages/cmake.rb', line 115

def cmake_cache
    File.join(builddir, "CMakeCache.txt")
end

#common_utility_handling(utility, target, start_msg, done_msg, post_process: nil, args: []) ⇒ Object

rubocop:disable Metrics/ParameterLists



281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
# File 'lib/autobuild/packages/cmake.rb', line 281

def common_utility_handling( # rubocop:disable Metrics/ParameterLists
    utility,
    target,
    start_msg,
    done_msg,
    post_process: nil,
    args: []
)
    utility.source_ref_dir = builddir
    utility.task do
        progress_start start_msg, :done_message => done_msg do
            if internal_doxygen_mode?
                run_doxygen
            else
                unless args.empty?
                    args = ["ARGS=#{args.map { |a| %("#{a}") }.join(' ')}"]
                end
                run(utility.name,
                    Autobuild.tool(:make),
                    "-j#{parallel_build_level}",
                    target, *args,
                    working_directory: builddir)
            end
            yield if block_given?
        end

        post_process&.call
    end
end

#configureObject

Configure the builddir directory before starting make



449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
# File 'lib/autobuild/packages/cmake.rb', line 449

def configure
    super do
        in_dir(builddir) do
            unless File.file?(File.join(srcdir, 'CMakeLists.txt'))
                raise ConfigException.new(self, 'configure'),
                      "#{srcdir} contains no CMakeLists.txt file"
            end

            command = ["cmake"]

            if Autobuild.windows?
                command << '-G'
                command << "MSYS Makefiles"
            end

            all_defines.each do |name, value|
                command << "-D#{name}=#{value}"
            end
            command << Array(generator).map { |g| "-G#{g}" } if generator
            command << srcdir

            progress_start "configuring CMake for %s",
                           done_message: "configured CMake for %s" do
                FileUtils.rm_f cmake_cache if full_reconfigures?
                run('configure', *command)
            end
        end
    end
end

#configurestampObject



119
120
121
# File 'lib/autobuild/packages/cmake.rb', line 119

def configurestamp
    cmake_cache
end

#coverage_blockObject



315
316
317
318
319
320
321
322
323
324
325
# File 'lib/autobuild/packages/cmake.rb', line 315

def coverage_block
    proc do
        next unless test_utility.coverage_enabled?
        next unless @with_coverage

        progress_start "generating coverage report for %s",
                       done_message: "generated coverage report for %s" do
            @with_coverage.call
        end
    end
end

#define(name, value) ⇒ Object



156
157
158
159
160
161
162
163
164
165
# File 'lib/autobuild/packages/cmake.rb', line 156

def define(name, value)
    @defines[name] =
        if value.respond_to?(:to_str)
            value.to_str
        elsif value
            'ON'
        else
            'OFF'
        end
end

#defines_changed?(all_defines, cache_data) ⇒ Boolean

Returns:

  • (Boolean)


399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
# File 'lib/autobuild/packages/cmake.rb', line 399

def defines_changed?(all_defines, cache_data)
    all_defines.any? do |name, value|
        if (match = /^#{name}:\w+=(.*)$/.match(cache_data))
            old_value = match[1]
        end

        value = value.to_s
        if !old_value || !equivalent_option_value?(old_value, value)
            if Autobuild.debug
                message "%s: option '#{name}' changed value: "\
                    "'#{old_value}' => '#{value}'"
            end

            if old_value
                message "%s: changed value of #{name} "\
                    "from #{old_value} to #{value}"
            else
                message "%s: setting value of #{name} to #{value}"
            end

            true
        end
    end
end

#delete_obsolete_filesObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Delete files in Package#prefix that are not present in CMake’s install manifest

This is enabled globally by delete_obsolete_files_in_prefix= or per-package with #delete_obsolete_files_in_prefix=. Do NOT enable if packages share the same prefix.



568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
# File 'lib/autobuild/packages/cmake.rb', line 568

def delete_obsolete_files
    # The expand_path is required to sanitize the paths, which can
    # contain e.g. double //
    cmake_install_manifest = File.join(builddir, 'install_manifest.txt')
    manifest_contents = File.readlines(cmake_install_manifest).
        map { |p| File.expand_path(p.chomp) }.to_set
    logdir = self.logdir
    counter = 0
    Find.find(prefix) do |path|
        Find.prune if path == logdir
        if !manifest_contents.include?(path) && File.file?(path)
            counter += 1
            FileUtils.rm path
        end
    end
    if counter > 0
        message "%s: removed #{counter} obsolete files from prefix (cmake)"
    end
end

#delete_obsolete_files_in_prefix?Boolean

Whether files that are not within CMake’s install manifest but are present in the prefix should be deleted. Note that the contents of #log_dir are unaffected.

It is false by default. Set to true only if each package has its own prefix.

Returns:

  • (Boolean)


132
133
134
# File 'lib/autobuild/packages/cmake.rb', line 132

def delete_obsolete_files_in_prefix?
    @delete_obsolete_files_in_prefix
end

#equivalent_option_value?(old, new) ⇒ Boolean

Returns:

  • (Boolean)


350
351
352
353
354
355
356
357
358
359
360
361
362
# File 'lib/autobuild/packages/cmake.rb', line 350

def equivalent_option_value?(old, new)
    if old == new
        true
    else
        old = CMAKE_EQVS[old]
        new = CMAKE_EQVS[new]
        if old && new
            old == new
        else
            false
        end
    end
end

#full_reconfigures?Boolean

If true, we always remove the CMake cache before reconfiguring. This is to workaround the aggressive caching behaviour of CMake, and is set to true by default.

See CMake.full_reconfigures? and CMake.full_reconfigures= for a global setting

Returns:

  • (Boolean)


107
108
109
110
111
112
113
# File 'lib/autobuild/packages/cmake.rb', line 107

def full_reconfigures?
    if @full_reconfigures.nil?
        CMake.full_reconfigures?
    else
        @full_reconfigures
    end
end

#import(**options) ⇒ Object



364
365
366
367
368
369
370
371
# File 'lib/autobuild/packages/cmake.rb', line 364

def import(**options)
    super

    Dir.glob(File.join(srcdir, "*.pc.in")) do |file|
        file = File.basename(file, ".pc.in")
        provides "pkgconfig/#{file}"
    end
end

#installObject

Install the result in prefix

If #delete_obsolete_files_in_prefix? is set, files that are present in the prefix but not in CMake’s install manifest will be removed.



548
549
550
551
552
553
554
555
556
557
558
# File 'lib/autobuild/packages/cmake.rb', line 548

def install
    in_dir(builddir) do
        progress_start "installing %s", done_message: 'installed %s' do
            run('install', Autobuild.tool(:make),
                "-j#{parallel_build_level}", 'install')
        end

        delete_obsolete_files if delete_obsolete_files_in_prefix?
    end
    super
end

#internal_doxygen_mode?Boolean

To avoid having to build packages to run the documentation target, we try to autodetect whether (1) the package is using doxygen and (2) whether the cmake variables in the doxyfile can be provided by autobuild itself.

This can be disabled globally by setting Autobuild::CMake.always_use_doc_target= or on a per-package basis with #always_use_doc_target=

This method returns true if the package can use the internal doxygen mode and false otherwise

Returns:

  • (Boolean)


237
238
239
240
241
242
243
244
245
246
247
248
249
# File 'lib/autobuild/packages/cmake.rb', line 237

def internal_doxygen_mode?
    return false if always_use_doc_target?

    doxyfile_in = File.join(srcdir, "Doxyfile.in")
    return false unless File.file?(doxyfile_in)

    File.readlines(doxyfile_in).each do |line|
        matches = line.scan(/@[^@]+@/)
        if matches.any? { |str| !DOXYGEN_ACCEPTED_VARIABLES.key?(str) }
            return false
        end
    end
end

#module_pathObject



373
374
375
# File 'lib/autobuild/packages/cmake.rb', line 373

def module_path
    CMake.module_path
end

#prefix_pathObject



377
378
379
380
381
382
383
384
385
386
387
388
389
390
# File 'lib/autobuild/packages/cmake.rb', line 377

def prefix_path
    seen = Set.new
    result = Array.new

    raw = (dependencies.map { |pkg_name| Autobuild::Package[pkg_name].prefix } +
        CMake.prefix_path)
    raw.each do |path|
        unless seen.include?(path)
            seen << path
            result << path
        end
    end
    result
end

#prepareObject



424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
# File 'lib/autobuild/packages/cmake.rb', line 424

def prepare
    # A failed initial CMake configuration leaves a CMakeCache.txt file,
    # but no Makefile.
    #
    # Delete the CMakeCache to force reconfiguration
    unless File.exist?(File.join(builddir, 'Makefile'))
        FileUtils.rm_f(cmake_cache)
    end

    doc_utility.source_ref_dir = builddir

    if File.exist?(cmake_cache)
        cache = File.read(cmake_cache)
        if defines_changed?(all_defines, cache)
            if Autobuild.debug
                message "%s: CMake configuration changed, forcing a reconfigure"
            end
            FileUtils.rm_f cmake_cache
        end
    end

    super
end

#run_doxygenObject

To avoid having to build packages to run the documentation target, we try to autodetect whether (1) the package is using doxygen and (2) whether the cmake variables in the doxyfile can be provided by autobuild itself.

This can be disabled globally by setting Autobuild::CMake.always_use_doc_target or on a per-package basis with #always_use_doc_target

This method generates the corresponding doxygen file in <builddir>/Doxygen and runs doxygen. It raises if the internal doxygen support cannot be used on this package



263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# File 'lib/autobuild/packages/cmake.rb', line 263

def run_doxygen
    doxyfile_in = File.join(srcdir, "Doxyfile.in")
    unless File.file?(doxyfile_in)
        raise "no Doxyfile.in in this package, "\
            "cannot use the internal doxygen support"
    end
    doxyfile_data = File.readlines(doxyfile_in).map do |line|
        line.gsub(/@[^@]+@/) do |match|
            DOXYGEN_ACCEPTED_VARIABLES[match].call(self)
        end
    end
    doxyfile = File.join(builddir, "Doxyfile")
    File.open(doxyfile, 'w') do |io|
        io.write(doxyfile_data)
    end
    run('doc', Autobuild.tool(:doxygen), doxyfile)
end

#self_fingerprintObject



588
589
590
591
592
593
# File 'lib/autobuild/packages/cmake.rb', line 588

def self_fingerprint
    return unless (base = super)

    all_defines = self.class.defines.merge(defines).sort_by(&:first)
    Digest::SHA1.hexdigest(base + all_defines.join(""))
end

#show_make_messages?Boolean

Returns:

  • (Boolean)


479
480
481
482
483
484
485
# File 'lib/autobuild/packages/cmake.rb', line 479

def show_make_messages?
    if !@show_make_messages.nil?
        @show_make_messages
    else
        CMake.show_make_messages?
    end
end

#update_environmentObject



392
393
394
395
396
397
# File 'lib/autobuild/packages/cmake.rb', line 392

def update_environment
    super
    prefix_path.each do |p|
        env_add_path 'CMAKE_PREFIX_PATH', p
    end
end

#with_coverage(&block) ⇒ Object



311
312
313
# File 'lib/autobuild/packages/cmake.rb', line 311

def with_coverage(&block)
    @with_coverage ||= block
end

#with_doc(target = 'doc', &block) ⇒ Object

Declare that the given target can be used to generate documentation



328
329
330
331
332
333
# File 'lib/autobuild/packages/cmake.rb', line 328

def with_doc(target = 'doc', &block)
    common_utility_handling(
        doc_utility, target,
        "generating documentation for %s",
        "generated documentation for %s", &block)
end

#with_tests(target = 'test', &block) ⇒ Object



335
336
337
338
339
340
341
342
# File 'lib/autobuild/packages/cmake.rb', line 335

def with_tests(target = 'test', &block)
    common_utility_handling(
        test_utility, target,
        "running tests for %s",
        "successfully ran tests for %s",
        args: test_args,
        post_process: coverage_block, &block)
end