#!/usr/bin/env ruby require 'como' include Como require 'rubu' include Rubu require 'fileutils' Spec.command( 'rubu_example', 'Tero Isannainen', '2018', [ [ :opt_multi, 'conf', '-c', "Configuration option." ], [ :switch, 'verbose', '-v', "Verbose." ], [ :switch, 'serial', '-s', "Serial execution." ], [ :default, nil, nil, "Trail(s) to execute." ], ] ) # ------------------------------------------------------------ # Build specific definitions: # Set trail related options. Var[ :bin_dir ] = 'bin' Var[ :source_dir ] = 'src' Var[ :gen_dir ] = 'gen_cee' Var[ :build_dir ] = 'build' Var[ :exe_name ] = 'build/hello' # Default for :fast option. Var[ :fast ] = false # Change Rubu to verbose mode. if Opt['verbose'].given Order[ :verbose ] = true end # Force serial execution. if Opt[ 'serial' ].given Order[ :serial ] = true end # Read setup files and apply Como command line arguments to Var space. Trail.setup( :como => 'conf' ) # ------------------------------------------------------------ # Collect sources and create targets: # Generator file and the target. gen_file = Mark.path( "#{Var[:bin_dir]}/gen_world" ) gen_cee_file = Mark.path( "#{Var[:gen_dir]}/world.c" ) # Collect "normal" C files. cee_files = Mark.glob( "#{Var[:source_dir]}/*.c" ) # Create complete list of all C files. all_cee_files = [ gen_cee_file ] + cee_files # Convert C files to corresponding Object files. obj_files = all_cee_files.peer( Var[ :build_dir ], '.o' ) # Specify executable file. exe_file = Mark.path( Var[ :exe_name ] ) # Generate execution time information to Info. Used by CleanObjects. Info[ :gen_files ] = [ gen_cee_file ] # ------------------------------------------------------------ # Build Commands: # Code generator with generated file dependency. class GenWorld < Step def step shrun "bin/gen_world" end end # Code generator with generated file content dependency. class GenGreenWorld < StepMark def step shrun "bin/gen_world" end end # Typical C compilation with GCC, from source to object. class GccCompileFile < StepAged def setup @cmd = "gcc -Wall -I #{Var[:source_dir]} -c #{source.rpath} -o #{target.rpath}" # Compile with "-O2" if :fast is set. if Var[ :fast ] @cmd += " -O2" else @cmd += " -g" end end def step shrun @cmd end end # GCC linking. class GccLinkExe < StepAged def setup @cmd = "gcc #{sources.rpath} -o #{target.rpath}" end def step shrun @cmd end end # Cleaning. class CleanObjects < Step def step # Multiple jobs are grouped with walk (serial execution). walk do shuse "rm -f #{Info[ :gen_files ].rpath}" shuse "rm -f #{Var[ :build_dir ]}/*.o" # Use Ruby for deleting the exe as an example. rbuse "rm -f #{Var[ :exe_name ]}" do FileUtils.rm_f Var[ :exe_name ] end end end end # ------------------------------------------------------------ # Define trails: # Serial trail for code generation. Walk.form 'gen-cee' do # Create GenWorld build and register it to 'gen-cee'. GenWorld.use( gen_file, gen_cee_file ) end # Serial trail for code generation (with filtered updates). Walk.form 'gen-cee-green' do # Create GenGreenWorld build and register it to 'gen-cee-green'. GenGreenWorld.use( gen_file, gen_cee_file ) end # Parallel compilation of all C files. Fork.form 'compile-cee' do # Create set of GCC builds by joining 'all_cee_files' and # 'obj_files' in pairs. GccCompileFile.usezip( all_cee_files, obj_files ) end # Link executable. Walk.form 'link' do GccLinkExe.use( obj_files, exe_file ) end # Default trail, i.e. generate, compile, link to executable. Walk.form 'default' do # Reference 'gen-cee' trail. pick 'gen-cee' # Reference 'compile-cee' trail. pick 'compile-cee' # Create GCC link build and register it. pick 'link' end # Trail for 'up-to-date' script (see: example dir). Walk.form 'up-to-date' do # Reference 'gen-cee-green' trail. pick 'gen-cee-green' # Reference 'compile-cee' trail. pick 'compile-cee' end # Cleaner trail. Walk.form 'clean' do CleanObjects.use end # ------------------------------------------------------------ # Run selected trails: trails = nil if Opt[ nil ].given trails = Opt[ nil ].value else trails = [ 'default' ] end Trail.run( trails )