Compare commits

..
153 Commits
Author SHA1 Message Date
josh c0adb64c5d v1.4.2 2014-05-09 10:32:22 -04:00
josh 95da14e029 add release notes in README.md 2014-05-09 10:31:54 -04:00
josh 58b41df426 expand target and sources for construction variable references before invoking builder 2014-05-09 09:45:29 -04:00
josh bd525e2742 rename Environment#clean_target_paths! -> #expand_paths! 2014-05-09 09:19:40 -04:00
josh c08a2243b9 add Environment#expand_path 2014-05-05 16:22:07 -04:00
josh 0de1e8e970 v1.4.1 2014-04-17 12:03:29 -04:00
josh 6f20a1da3f bugfix: support invoking builders with no sources and a build root defined 2014-04-17 12:03:09 -04:00
josh 03ffb39956 v1.4.0 2014-04-15 16:19:19 -04:00
josh 0a10af3f5e update README.md for v1.4.0 release notes 2014-04-15 16:18:58 -04:00
josh ddf00d08a7 shortcut all of Environment#process if @targets is empty 2014-04-15 16:18:33 -04:00
josh 8be1842cf8 update documentation for new builders 2014-04-15 16:06:00 -04:00
josh d424facf7f Add Preprocess and Disassemble builders 2014-04-15 15:21:55 -04:00
josh ee5aca275d Write Cache to disk if an exception occurs while processing the Environment - fix #4 2014-04-15 14:20:30 -04:00
josh ea30b0b5ee Fix Cache#mkdir_p to handle absolute paths - fix #5 2014-04-15 14:15:32 -04:00
josh 5a0d80b781 Cache: implement "dirty" attribute to only re-serialize when necessary 2014-04-15 14:11:10 -04:00
josh a2653a77d1 shortcut Environment#process if @targets is empty 2014-04-15 14:04:59 -04:00
josh 160434b903 Cache: change to a Singleton object so it is only loaded from disk once 2014-04-15 14:02:26 -04:00
josh 5ce8940d54 stop encoding Cache contents 2014-04-15 13:39:45 -04:00
josh 1a5e9ddaa6 Implement copy-on-write semantics for VarSet, speeding up Environment cloning 2014-04-15 11:21:15 -04:00
josh 5924154229 speed up VarSet#deep_dup a bit 2014-04-15 09:47:05 -04:00
josh 5beda54374 do not clone @varset when appending to the cloned environment since appending will do its own clone 2014-04-15 09:36:30 -04:00
josh ae0ad98075 use @varset.append() instead of @varset.send(:append) 2014-04-15 09:32:35 -04:00
josh f5be3a3571 VarSet: implement deep copying ourselves instead of using Marshal for a speed increase 2014-04-15 09:16:30 -04:00
josh fdc5c8773c Issue #7 - store MD5 of build command instead of the full command in the cache 2014-04-14 17:17:54 -04:00
josh c61380b354 Issue #7 - change .rsconscache from using YAML to JSON to speed up parsing 2014-04-14 16:52:21 -04:00
josh d8dda291a5 ensure that Dir[] results are predictable in build tests 2014-04-14 13:13:43 -04:00
josh 1fe2c6401d expand ^/ in target paths to be relative to the build root 2014-03-27 13:30:11 -04:00
josh 4818fd10fe README.md: add more details about Environment cloning 2014-03-27 12:02:46 -04:00
josh 5989e24ee6 update some documentation 2014-03-27 11:57:45 -04:00
josh 1875a2ea66 Pass the Environment object to build hooks in the :env key of the build_op parameter 2014-03-27 10:44:10 -04:00
josh cbd5816b4b add VarSet#include? 2014-03-27 10:39:46 -04:00
josh b2c970c9ca add Environment#add_target and use it from Environment#method_missing 2014-03-26 11:29:02 -04:00
josh 92a9982988 use #super from #method_missing instead of method aliasing 2014-03-26 11:23:32 -04:00
josh 533ee9c16c add CFile builder to create C/C++ sources from lex/yacc input files 2014-03-18 21:57:53 -04:00
josh 831b9bf2c7 v1.3.0 2014-02-25 12:18:02 -05:00
josh 031395bb57 add v1.3.0 release notes 2014-02-25 12:17:49 -05:00
josh e3aeb7f4d7 Change Environment#execute() options Hash to contain :env and :options keys for Kernel#system() 2014-02-25 12:14:52 -05:00
josh 6aa5037234 v1.2.0 2014-02-20 16:42:10 -05:00
josh 0d8f661fd4 add :clone option to Environment#clone to control exactly which Environment attributes are cloned 2014-02-19 15:02:46 -05:00
josh e5ca8776a9 allow nil to be passed in to Environment#build_root= 2014-02-19 15:02:18 -05:00
josh 2dde39a0ec standardize on "Rscons" capitalization 2014-02-18 16:14:44 -05:00
josh af0679708c v1.1.0 2014-02-17 16:37:59 -05:00
josh 4b0d0c3957 README: add Release Notes section 2014-02-17 16:15:49 -05:00
josh 202f10b86f README: add example for custom builder with multiple output files 2014-02-17 16:14:10 -05:00
josh 1d47db3144 support passing multiple targets to Cache#up_to_date? and #register_build 2014-02-17 16:10:15 -05:00
josh f7b7a4fefa v1.0.0 2014-02-14 10:46:31 -05:00
josh 00b038ef50 use require_relative instead of require 2014-02-14 10:37:41 -05:00
josh 98f8f38715 remove String#set_suffix monkey patch 2014-02-14 10:36:15 -05:00
josh 2ffb5d525a remove String#absolute_path? monkey patch 2014-02-14 10:32:57 -05:00
josh fa7d17675b remove String#has_suffix? monkey-patch; use String#end_with? 2014-02-14 10:20:32 -05:00
josh b43c473762 README: add link to API documentation at rubydoc.info 2014-02-14 09:57:46 -05:00
josh f278b211f7 README: add more information about custom builders 2014-02-14 09:50:39 -05:00
josh c897bcf06e README.md fixes/tweaks 2014-02-14 09:42:04 -05:00
josh 7c89dddbd6 v0.3.1 2014-01-23 14:40:45 -05:00
josh c07d9f86a6 bugfix: loading a Cache without deps or user_deps stored causes an exception 2014-01-23 14:40:30 -05:00
josh ff26da8865 fix custom builder examples in README.md 2014-01-15 16:57:46 -05:00
josh 5ff555cf6a add Environment#expand_varref() 2014-01-02 11:49:08 -05:00
josh e3aadc562f Rscons::Varset: remove unused type parameter to #[] 2014-01-02 11:05:15 -05:00
josh 7ff2ec1f1a v0.3.0 2013-12-31 14:10:47 -05:00
josh 5362f761e6 move default builder classes into Rscons::Builders namespace module 2013-12-31 13:31:17 -05:00
josh cdb3352b4e remove module monkey-patching; add Builder#name() 2013-12-31 13:10:00 -05:00
josh 08bc390d40 Rscons::BuildError inherits from RuntimeError instead of Exception 2013-12-31 13:08:38 -05:00
josh f8af0630b0 pass env to Cache#up_to_date?() and #register_build() instead of user_deps 2013-12-31 12:46:30 -05:00
josh 1868193b54 do not prepend build_root to source files that are already inside build_root 2013-12-31 10:15:19 -05:00
josh 8976d7dea2 update README custom builder examples 2013-12-27 15:01:02 -05:00
josh 295324eafd add Environment#depends() to specify user dependencies for a build target 2013-12-27 14:31:46 -05:00
josh 7330e74ab8 modify Cache to store user-specified extra dependencies 2013-12-27 14:20:32 -05:00
josh a28cef9eb7 change "vars" parameters to be required 2013-12-27 13:53:40 -05:00
josh e667046da8 add Environment#clear_targets() 2013-12-27 13:35:16 -05:00
josh 539a49fe90 do not allow construction variables as arguments to Environment#initialize() or Environment#clone() 2013-12-27 12:10:54 -05:00
josh 7681c7584f add String#absolute_path? 2013-12-27 11:40:24 -05:00
josh 8fcbd39057 v0.2.2 2013-11-13 23:52:33 -05:00
josh d0bbc78751 Program: do not continue executing builder if building a source fails 2013-11-13 23:52:15 -05:00
josh 7249e57951 add info to error message when expanding unknown variable types 2013-11-13 23:50:46 -05:00
josh d01913641f v0.2.1 2013-11-11 11:36:10 -05:00
josh d5be569cd5 fix Environment#clone() to allow subclassing 2013-11-11 11:35:59 -05:00
josh 4d7ed373e7 v0.2.0 2013-11-08 15:45:35 -05:00
josh 72558391a2 clarify documentation about cloning Environments 2013-11-08 15:45:24 -05:00
josh d01767981c add Environment#build_root to be used if no build directories match 2013-11-08 14:52:39 -05:00
josh 01c847fa32 v0.1.0 2013-11-06 13:38:59 -05:00
josh 20f509ec27 rename *COM construction variables to *CMD 2013-11-06 13:35:41 -05:00
josh 243eb9a4ab rename "tweakers" to "build hooks" 2013-11-06 13:32:56 -05:00
josh 49be46199f define our own clean/clobber tasks 2013-11-06 13:29:24 -05:00
josh c9b670ae5c change default "echo" setting to :short 2013-11-06 13:22:30 -05:00
josh 6bcb984e8e README: add gem version badge 2013-11-06 13:18:09 -05:00
josh b377940252 begin expanding README documentation 2013-11-06 12:45:50 -05:00
josh 2e0c2688aa VarSet#expand_varref(): fix recursive expansion 2013-11-05 17:06:09 -05:00
josh 66f53a23f6 change construction variable reference format to ${var}
this change is backwards-incompatible
2013-11-05 16:54:01 -05:00
josh 545b893ef4 clean up a couple YARD warnings 2013-11-05 16:37:18 -05:00
josh 30564af558 add build test for D 2013-11-05 16:29:15 -05:00
josh cc2c2733fc move build_tests_spec.rb back into spec, remove build_tests rake task 2013-11-05 16:22:09 -05:00
josh ddb5357b02 explicitly set LD construction variable in one build test 2013-11-05 16:19:02 -05:00
josh b805e5379f execute all build tests in the top-level rake context instead of spawning subprocesses 2013-11-05 16:11:08 -05:00
josh 71344b4782 Program: do not check env.build_sources() return value (it will raise an exception if something was wrong) 2013-11-05 15:41:47 -05:00
josh b1cdd3d1db Object: simplify handling of known suffixes 2013-11-05 15:25:10 -05:00
josh ed11b05b97 add rscons_spec.rb 2013-11-05 14:37:26 -05:00
josh a418d31000 finish up Environment specs 2013-11-05 14:28:43 -05:00
josh 797580e937 Environment: remove support for specifying :builders key in constructor 2013-11-05 14:18:02 -05:00
josh 8b852bd2fd Environment: more specs 2013-11-05 14:14:03 -05:00
josh e863c9a564 Environment: rework process()
- improve efficiency
- give errors for the dependency that failed instead of the top-level target
2013-11-05 12:43:50 -05:00
josh 7ce5a8f9d9 working on Environment specs 2013-11-05 12:26:42 -05:00
josh 1280cfb465 change Environment#parse_makefile_deps() to a class method 2013-11-05 11:44:07 -05:00
josh 0cd9dbd1bd add separate spec_helper.rb for build_tests directory 2013-11-05 11:36:30 -05:00
josh b38c56d446 move build_tests_spec.rb to build_tests directory; add build_tests task 2013-11-05 11:05:45 -05:00
josh d2b7c93f53 rename Rakefile -> Rakefile.rb 2013-11-05 10:52:56 -05:00
josh 67922b887d remove Environment#targets() and Environment#target_sources() which didn't seem to be used 2013-11-05 10:42:01 -05:00
josh 8100830c16 spec Cache 2013-11-05 10:25:36 -05:00
josh cb629bf7ec do not include spec files in simplecov report 2013-11-05 10:25:34 -05:00
josh 65a5838605 Cache: set :version when writing to file 2013-11-05 09:22:46 -05:00
josh 338a49ba9c v0.0.14 2013-10-29 16:18:14 -04:00
josh c23bb8c5e0 add support for building D sources (defaulting to gdc) 2013-10-29 16:17:51 -04:00
josh 31abeed3d5 v0.0.13 2013-10-15 16:29:54 -04:00
josh 446fef931f add "tweakers" functionality
tweakers allow the user to modify construction variables before any build is performed
2013-10-15 16:15:37 -04:00
josh 4a2997feec VarSet: make a deep copy of values passed to VarSet#append() 2013-10-15 16:10:56 -04:00
josh 013995bbc0 refactor into Environment#run_builder() 2013-10-15 13:36:25 -04:00
josh cd266c511e v0.0.12 2013-10-08 11:54:38 -04:00
josh 0f5fee359d cloning an Environment should make a deep copy of its construction variables 2013-10-08 11:54:24 -04:00
josh dc6bac2895 change build_dir build test to test build_dir specified as a Regexp 2013-10-04 21:40:11 -04:00
josh e50aac31c9 tweak custom_builder build test for additional coverage 2013-10-04 21:26:51 -04:00
josh e788f6ec25 claim simplecov coverage from build_tests 2013-10-04 21:05:44 -04:00
josh 8d893d7b72 rspec Module monkey-patching 2013-10-04 20:22:37 -04:00
josh c258dc379f rspec VarSet 2013-10-04 20:18:40 -04:00
josh 0facfb274d turn on simplecov 2013-10-04 19:37:56 -04:00
josh 3339cf5280 Cache: re-encode file names in default encoding 2013-09-20 15:58:55 -04:00
josh e041ea6612 v0.0.11 2013-09-19 17:06:37 -04:00
josh ab51ddcc1b do not try to clean directories that have already been removed 2013-09-19 17:06:20 -04:00
josh 3e4e9cce1c v0.0.10 2013-09-19 16:48:15 -04:00
josh 32d19489cb Cache: encode directory names in __ENCODING__ - close #3 2013-09-19 16:45:35 -04:00
josh 45a0d56c54 recover from, and warn when, .rsconscache loads something other than a Hash 2013-09-19 16:18:07 -04:00
josh 9b954d9d98 v0.0.9 2013-09-19 09:43:17 -04:00
josh e63b4c932f Cache: create missing hash entries from previous versions of the cache file 2013-09-19 09:43:00 -04:00
josh 97e11f45e5 v0.0.8 2013-09-18 21:18:00 -04:00
josh 5c6239ea6d Rscons.clean() removes any directories it created if they are empty 2013-09-18 21:17:42 -04:00
josh 770a22250a add Rscons.clear() class method 2013-09-17 21:48:50 -04:00
josh 983cb7cf21 v0.0.7 2013-09-16 23:03:14 -04:00
josh 986b8f08cc prepend builder-temporary construction variables with "_"; reserve lowercase string variables for user 2013-09-16 22:30:16 -04:00
josh f13664331f create requisite output directories for build targets; refactor common buile functionality into Builder#standard_build() 2013-09-15 15:27:16 -04:00
josh 17c7bf1d31 v0.0.6 2013-09-12 23:37:37 -04:00
josh 64d15602cc add Library default builder 2013-09-12 23:35:34 -04:00
josh e04e67698a add rspec test for overridding construction variables for an individual builer 2013-09-12 23:05:04 -04:00
josh 837dff9374 pass extra construction variables to Builder#run() 2013-09-12 22:57:22 -04:00
josh 75f2418570 refactor some Program functionality into Environment#build_sources() 2013-09-12 22:38:52 -04:00
josh 711e96cec8 write out cache file when raising a build error exception 2013-08-12 14:40:46 -04:00
josh 289d27d41d raise Rscons::BuildError on a build failure 2013-08-12 13:13:22 -04:00
josh 052b1afe19 v0.0.3 2013-08-05 17:19:22 -04:00
josh 72976be765 fix verifying a target's checksum 2013-08-05 17:19:08 -04:00
josh ad2424007f v0.0.2 2013-08-05 14:56:20 -04:00
josh c3c6385068 String#has_suffix?(): allow nested arrays of suffixes 2013-08-05 14:51:07 -04:00
josh 13b9499fbf Environment#build_dir(): allow src_dir to be a regex 2013-08-05 14:48:47 -04:00
josh 4286925f95 Environment#clone(): do not clone build directories 2013-08-05 14:48:31 -04:00
josh 0db5614555 rename LIBPATHS default construction variable to LIBPATH 2013-08-05 14:34:59 -04:00
josh 5d0bbf73ab specify gem homepage and license - close #1 2013-08-05 14:31:48 -04:00
josh 75977981c0 fix parsing Makefile dependencies when they spanned multiple lines 2013-08-05 14:27:57 -04:00
37 changed files with 2449 additions and 445 deletions

No files matched your search

+1 -1
View File
@@ -15,4 +15,4 @@ spec/reports
test/tmp test/tmp
test/version_tmp test/version_tmp
tmp tmp
build_tests_run build_test_run
+350 -13
View File
@@ -2,15 +2,17 @@
Software construction library inspired by SCons and implemented in Ruby Software construction library inspired by SCons and implemented in Ruby
[![Gem Version](https://badge.fury.io/rb/rscons.png)](http://badge.fury.io/rb/rscons)
## Installation ## Installation
Add this line to your application's Gemfile: Add this line to your application's Gemfile:
gem 'rscons' gem "rscons"
And then execute: And then execute:
$ bundle $ bundle install
Or install it yourself as: Or install it yourself as:
@@ -18,20 +20,355 @@ Or install it yourself as:
## Usage ## Usage
RScons::Environment.new do |env| Rscons is a Ruby library.
env.Program('program', Dir['*.c']) It can be called from a standalone Ruby script or it can be used with rake and
end called from your Rakefile.
main_env = RScons::Environment.new do |env| ### Example: Building a C Program
env.build_dir('src', 'build/main')
env.Program('program', Dir['src/**/*.cc'])
end
debug_env = main_env.clone do |env| ```ruby
env.build_dir('src', 'build/debug') Rscons::Environment.new do |env|
env['CFLAGS'] = ['-g', '-O0'] env["CFLAGS"] << "-Wall"
env.Program('program-debug', Dir['src/**/*.cc']) env.Program("program", Dir["**/*.c"])
end
```
### Example: Building a D Program
```ruby
Rscons::Environment.new do |env|
env["DFLAGS"] << "-Wall"
env.Program("program", Dir["**/*.d"])
end
```
### Example: Cloning an Environment
```ruby
main_env = Rscons::Environment.new do |env|
# Store object files from sources under "src" in "build/main"
env.build_dir("src", "build/main")
env["CFLAGS"] = ["-DSOME_DEFINE", "-O3"]
env["LIBS"] = ["SDL"]
env.Program("program", Dir["src/**/*.cc"])
end
debug_env = main_env.clone do |env|
# Store object files from sources under "src" in "build/debug"
env.build_dir("src", "build/debug")
env["CFLAGS"] -= ["-O3"]
env["CFLAGS"] += ["-g", "-O0"]
env.Program("program-debug", Dir["src/**/*.cc"])
end
```
### Example: Custom Builder
Custom builders are implemented as classes which extend from `Rscons::Builder`.
The builder must have a `run` method which is called to invoke the builder.
The `run` method should return the name of the target built on success, and
`false` on failure.
```ruby
class GenerateFoo < Rscons::Builder
def run(target, sources, cache, env, vars)
cache.mkdir_p(File.dirname(target))
File.open(target, "w") do |fh|
fh.puts <<EOF
#define GENERATED 42
EOF
end end
target
end
end
Rscons::Environment.new do |env|
env.add_builder(GenerateFoo.new)
env.GenerateFoo("foo.h", [])
env.Program("a.out", Dir["*.c"])
end
```
### Example: Custom Builder That Only Regenerates When Necessary
```ruby
class CmdBuilder < Rscons::Builder
def run(target, sources, cache, env, vars)
cmd = ["cmd", "-i", sources.first, "-o", target]
unless cache.up_to_date?(target, cmd, sources, env)
cache.mkdir_p(File.dirname(target))
system(cmd)
cache.register_build(target, cmd, sources, env)
end
target
end
end
Rscons::Environment.new do |env|
env.add_builder(CmdBuilder.new)
env.CmdBuilder("foo.gen", "foo_gen.cfg")
end
```
### Example: Custom Builder That Generates Multiple Output Files
```ruby
class CModuleGenerator < Rscons::Builder
def run(target, sources, cache, env, vars)
c_fname = target
h_fname = target.sub(/\.c$/, ".h")
cmd = ["generate_c_and_h", sources.first, c_fname, h_fname]
unless cache.up_to_date?([c_fname, h_fname], cmd, sources, env)
cache.mkdir_p(File.dirname(target))
system(cmd)
cache.register_build([c_fname, h_fname], cmd, sources, env)
end
target
end
end
Rscons::Environment.new do |env|
env.add_builder(CModuleGenerator.new)
env.CModuleGenerator("build/foo.c", "foo_gen.cfg")
end
```
### Example: Custom Builder Using Builder#standard_build()
The `standard_build` method from the `Rscons::Builder` base class can be used
when the builder needs to execute a system command to produce the target file.
The `standard_build` method will return the correct value so its return value
can be used as the return value from the `run` method.
```ruby
class CmdBuilder < Rscons::Builder
def run(target, sources, cache, env, vars)
cmd = ["cmd", "-i", sources.first, "-o", target]
standard_build("CmdBld #{target}", target, cmd, sources, env, cache)
end
end
Rscons::Environment.new do |env|
env.add_builder(CmdBuilder.new)
env.CmdBuilder("foo.gen", "foo_gen.cfg")
end
```
### Example: Using different compilation flags for some sources
```ruby
Rscons::Environment.new do |env|
env["CFLAGS"] = ["-O3", "-Wall", "-DDEFINE"]
env.add_build_hook do |build_op|
if build_op[:target] =~ %r{build/third-party}
build_op[:vars]["CFLAGS"] -= ["-Wall"]
end
end
env.build_dir("src", "build")
env.Program("program", Dir["**/*.cc"])
end
```
Each build hook block will be invoked for every build operation, so the block
should test the target or sources if its action should only apply to some
subset of build targets or source files.
The `build_op` parameter to the build hook block is a Hash describing the
build operation with the following keys:
* `:builder` - `Builder` instance in use
* `:env` - `Environment` calling the build hook; note that this may be
different from the Environment that the build hook was added to in the case
that the original Environment was cloned with build hooks!
* `:target` - `String` name of the target file
* `:sources` - `Array` of the source files
* `:vars` - `Rscons::VarSet` containing the construction variables to use.
The build hook can overwrite entries in `build_op[:vars]` to alter the
construction variables in use for this specific build operation.
### Example: Creating a static library
```ruby
Rscons::Environment.new do |env|
env.Library("mylib.a", Dir["src/**/*.c"])
end
```
### Example: Creating a C++ parser source from a Yacc/Bison input file
```ruby
Rscons::Environment.new do |env|
env.CFile("#{env.build_root}/parser.tab.cc", "parser.yy")
end
```
## Details
### Builders
Rscons ships with a number of builders:
* CFile, which builds a C or C++ source file from a lex or yacc input file
* Disassemble, which disassembles an object file to a disassembly listing
* Library, which collects object files into a static library archive file
* Object, which compiles source files to produce an object file
* Preprocess, which invokes the C/C++ preprocessor on a source file
* Program, which links object files to produce an executable
If you want to create an Environment that does not contain any builders,
you can use the `exclude_builders` key to the Environment constructor.
#### CFile
```ruby
env.CFile(target, source)
# Example
env.CFile("parser.c", "parser.y")
```
The CFile builder will generate a C or C++ source file from a lex (.l, .ll)
or yacc (.y, .yy) input file.
#### Disassemble
```ruby
env.Disassemble(target, source)
# Example
env.Disassemble("module.dis", "module.o")
```
The Disassemble builder generates a disassembly listing using objdump from
and object file.
#### Library
```ruby
env.Library(target, sources)
# Example
env.Library("lib.a", Dir["src/**/*.c"])
```
The Library builder creates a static library archive from the given source
files.
#### Object
```ruby
env.Object(target, sources)
# Example
env.Object("module.o", "module.c")
```
The Object builder compiles the given sources to an object file.
#### Preprocess
```ruby
env.Preprocess(target, source)
# Example
env.Preprocess("module-preprocessed.cc", "module.cc")
```
The Preprocess builder invokes either ${CC} or ${CXX} (depending on if the
source contains an extension in ${CXXSUFFIX} or not) and writes the
preprocessed output to the target file.
#### Program
```ruby
env.Program(target, sources)
# Example
env.Program("myprog", Dir["src/**/*.cc"])
```
The Program builder compiles and links the given sources to an executable file.
Object files or source files can be given as `sources`.
### Managing Environments
An Rscons::Environment consists of:
* a collection of construction variables
* a collection of builders
* a mapping of build directories from source directories
* a default build root to apply if no build directories are matched
* a collection of targets to build
* a collection of build hooks
When cloning an environment, by default the construction variables and builders
are cloned, but the new environment does not inherit any of the targets, build
hooks, build directories, or the build root from the source environment.
The set of environment attributes that are cloned is controllable via the
`:clone` option to the `#clone` method.
For example, `env.clone(clone: :all)` will include construction variables,
builders, build hooks, build directories, and the build root.
The set of pending targets is never cloned.
Cloned environments contain "deep copies" of construction variables.
For example, in:
```ruby
base_env = Rscons::Environment.new
base_env["CPPPATH"] = ["one", "two"]
cloned_env = base_env.clone
cloned_env["CPPPATH"] << "three"
```
`base_env["CPPPATH"]` will not include "three".
### Construction Variable Naming
* uppercase strings - the default construction variables that Rscons uses
* symbols, lowercase strings - reserved as user-defined construction variables
### API documentation
Documentation for the complete Rscons API can be found at
http://rubydoc.info/github/holtrop/rscons/frames.
## Release Notes
### v1.4.2
- add Environment#expand_path
- expand construction variable references in builder targets and sources before invoking builder
### v1.4.1
- fix invoking a builder with no sources while a build root defined
### v1.4.0
- add CFile builder
- add Disassemble builder
- add Preprocess builder
- pass the Environment object to build hooks in the :env key of the build_op parameter
- expand target/source paths beginning with "^/" to be relative to the Environment's build root
- many performance improvements, including:
- use JSON instead of YAML for the cache to improve loading speed (Issue #7)
- store a hash of the build command instead of the full command contents in the cache
- implement copy-on-write semantics for construction variables when cloning Environments
- only load the cache once instead of on each Environment#process
- only write the cache when something has changed
- fix Cache#mkdir_p to handle relative paths (Issue #5)
- flush the cache to disk if a builder raises an exception (Issue #4)
### v1.3.0
- change Environment#execute() options parameter to accept the following options keys:
- :env to pass an environment Hash to Kernel#system
- :options to pass an options Hash to Kernel#system
### v1.2.0
- add :clone option to Environment#clone to control exactly which Environment attributes are cloned
- allow nil to be passed in to Environment#build_root=
### v1.1.0
- Change Cache#up_to_date?() and #register_build() to accept a single target
file or an array of target file names
## Contributing ## Contributing
+8 -3
View File
@@ -7,11 +7,8 @@ end
require "bundler/gem_tasks" require "bundler/gem_tasks"
require "rspec/core/rake_task" require "rspec/core/rake_task"
require "rake/clean"
require "yard" require "yard"
CLEAN.include 'build_tests_run'
RSpec::Core::RakeTask.new(:spec) RSpec::Core::RakeTask.new(:spec)
YARD::Rake::YardocTask.new do |yard| YARD::Rake::YardocTask.new do |yard|
@@ -19,3 +16,11 @@ YARD::Rake::YardocTask.new do |yard|
end end
task :default => :spec task :default => :spec
task :clean do
FileUtils.rm_rf(["build_test_run", "doc", "coverage"])
end
task :clobber => :clean do
FileUtils.rm_rf(["pkg"])
end
-5
View File
@@ -1,5 +0,0 @@
Rscons::Environment.new do |env|
env.append('CPPPATH' => Dir['src/**/*/'])
env.build_dir('src', 'build')
env.Program('build_dir', Dir['src/**/*.c'])
end
-11
View File
@@ -1,11 +0,0 @@
debug = Rscons::Environment.new do |env|
env.build_dir('src', 'debug')
env['CFLAGS'] = '-O2'
env['CPPFLAGS'] = '-DSTRING="Debug Version"'
env.Program('program-debug', Dir['src/*.c'])
end
release = debug.clone('CPPFLAGS' => '-DSTRING="Release Version"') do |env|
env.build_dir('src', 'release')
env.Program('program-release', Dir['src/*.c'])
end
-16
View File
@@ -1,16 +0,0 @@
class MySource < Rscons::Builder
def run(target, sources, cache, env)
File.open(target, 'w') do |fh|
fh.puts <<EOF
#define THE_VALUE 5678
EOF
end
target
end
end
Rscons::Environment.new(echo: :short) do |env|
env.add_builder(MySource.new)
env.MySource('inc.h', [])
env.Program('program', Dir['*.c'])
end
+6
View File
@@ -0,0 +1,6 @@
import std.stdio;
void main()
{
writeln("Hello from D!");
}
-3
View File
@@ -1,3 +0,0 @@
Rscons::Environment.new(echo: :short) do |env|
env.Program('header', Dir['*.c'])
end
+8
View File
@@ -0,0 +1,8 @@
#include <stdio.h>
#ifdef make_lib
int main(int argc, char *argv[])
{
printf("Library\n");
}
#endif
View File
Whitespace-only changes.
View File
Whitespace-only changes.
-4
View File
@@ -1,4 +0,0 @@
Rscons::Environment.new do |env|
# CHANGE FLAGS
env.Program('simple', Dir['*.c'])
end
-4
View File
@@ -1,4 +0,0 @@
Rscons::Environment.new do |env|
# CHANGE FLAGS
env.Program('simple', Dir['*.cc'])
end
+8
View File
@@ -0,0 +1,8 @@
#include <stdio.h>
#ifdef ONE
int main(int argc, char *argv[])
{
printf("This is a C program with two sources.\n");
}
#endif
+3
View File
@@ -0,0 +1,3 @@
#ifdef ONE
#error ONE should not be defined
#endif
+49 -12
View File
@@ -1,20 +1,57 @@
require "rscons/builder" require_relative "rscons/builder"
require "rscons/cache" require_relative "rscons/cache"
require "rscons/environment" require_relative "rscons/environment"
require "rscons/varset" require_relative "rscons/varset"
require "rscons/version" require_relative "rscons/version"
require "rscons/monkey/module"
require "rscons/monkey/string"
# default builders # default builders
require "rscons/builders/object" require_relative "rscons/builders/cfile"
require "rscons/builders/program" require_relative "rscons/builders/disassemble"
require_relative "rscons/builders/library"
require_relative "rscons/builders/object"
require_relative "rscons/builders/preprocess"
require_relative "rscons/builders/program"
# Namespace module for rscons classes # Namespace module for rscons classes
module Rscons module Rscons
DEFAULT_BUILDERS = [ DEFAULT_BUILDERS = [
Object, :CFile,
Program, :Disassemble,
:Library,
:Object,
:Preprocess,
:Program,
] ]
class BuildError < RuntimeError; end
# Remove all generated files
def self.clean
cache = Cache.instance
# remove all built files
cache.targets.each do |target|
FileUtils.rm_f(target)
end
# remove all created directories if they are empty
cache.directories.sort {|a, b| b.size <=> a.size}.each do |directory|
next unless File.directory?(directory)
if (Dir.entries(directory) - ['.', '..']).empty?
Dir.rmdir(directory) rescue nil
end
end
cache.clear
end
# Return whether the given path is an absolute filesystem path or not
# @param path [String] the path to examine
def self.absolute_path?(path)
path =~ %r{^(/|\w:[\\/])}
end
# Return a new path by changing the suffix in path to suffix.
# @param path [String] the path to alter
# @param suffix [String] the new filename suffix
def self.set_suffix(path, suffix)
path.sub(/\.[^.]*$/, suffix)
end
end end
+24
View File
@@ -1,6 +1,17 @@
require "fileutils"
module Rscons module Rscons
# Namespace module in which to store builders for convenient grouping
module Builders; end
# Class to hold an object that knows how to build a certain type of file. # Class to hold an object that knows how to build a certain type of file.
class Builder class Builder
# Return the name of the builder.
# If not overridden this defaults to the last component of the class name.
def name
self.class.name.split(":").last
end
# Return a set of default variable values for the Environment to use # Return a set of default variable values for the Environment to use
# unless the user overrides any. # unless the user overrides any.
# @param env [Environment] The Environment. # @param env [Environment] The Environment.
@@ -16,5 +27,18 @@ module Rscons
def produces?(target, source, env) def produces?(target, source, env)
false false
end end
# Check if the cache is up to date for the target and if not execute the
# build command.
# Return the name of the target or false on failure.
def standard_build(short_cmd_string, target, command, sources, env, cache)
unless cache.up_to_date?(target, command, sources, env)
cache.mkdir_p(File.dirname(target))
FileUtils.rm_f(target)
return false unless env.execute(short_cmd_string, command)
cache.register_build(target, command, sources, env)
end
target
end
end end
end end
+40
View File
@@ -0,0 +1,40 @@
module Rscons
module Builders
# Build a C or C++ source file given a lex (.l, .ll) or yacc (.y, .yy)
# input file.
#
# Examples::
# env.CFile("parser.tab.cc", "parser.yy")
# env.CFile("lex.yy.cc", "parser.ll")
class CFile < Builder
def default_variables(env)
{
"YACC" => "bison",
"YACC_FLAGS" => ["-d"],
"YACC_CMD" => ["${YACC}", "${YACC_FLAGS}", "-o", "${_TARGET}", "${_SOURCES}"],
"LEX" => "flex",
"LEX_FLAGS" => [],
"LEX_CMD" => ["${LEX}", "${LEX_FLAGS}", "-o", "${_TARGET}", "${_SOURCES}"],
}
end
def run(target, sources, cache, env, vars)
vars = vars.merge({
"_TARGET" => target,
"_SOURCES" => sources,
})
cmd =
case
when sources.first.end_with?(".l", ".ll")
"LEX"
when sources.first.end_with?(".y", ".yy")
"YACC"
else
raise "Unknown source file #{sources.first.inspect} for CFile builder"
end
command = env.build_command(env["#{cmd}_CMD"], vars)
standard_build("#{cmd} #{target}", target, command, sources, env, cache)
end
end
end
end
+25
View File
@@ -0,0 +1,25 @@
module Rscons
module Builders
# The Disassemble builder produces a disassembly listing of a source file.
class Disassemble < Builder
def default_variables(env)
{
"OBJDUMP" => "objdump",
"DISASM_CMD" => ["${OBJDUMP}", "${DISASM_FLAGS}", "${_SOURCES}"],
"DISASM_FLAGS" => ["--disassemble", "--source"],
}
end
def run(target, sources, cache, env, vars)
vars = vars.merge("_SOURCES" => sources)
command = env.build_command(env["DISASM_CMD"], vars)
unless cache.up_to_date?(target, command, sources, env)
cache.mkdir_p(File.dirname(target))
return false unless env.execute("Disassemble #{target}", command, options: {out: target})
cache.register_build(target, command, sources, env)
end
target
end
end
end
end
+28
View File
@@ -0,0 +1,28 @@
module Rscons
module Builders
# A default Rscons builder that produces a static library archive.
class Library < Builder
def default_variables(env)
{
'AR' => 'ar',
'LIBSUFFIX' => '.a',
'ARFLAGS' => [],
'ARCMD' => ['${AR}', 'rcs', '${ARFLAGS}', '${_TARGET}', '${_SOURCES}']
}
end
def run(target, sources, cache, env, vars)
# build sources to linkable objects
objects = env.build_sources(sources, [env['OBJSUFFIX'], env['LIBSUFFIX']].flatten, cache, vars)
if objects
vars = vars.merge({
'_TARGET' => target,
'_SOURCES' => objects,
})
command = env.build_command(env['ARCMD'], vars)
standard_build("AR #{target}", target, command, objects, env, cache)
end
end
end
end
end
+73 -61
View File
@@ -1,69 +1,81 @@
module Rscons module Rscons
# A default RScons builder which knows how to produce an object file from module Builders
# various types of source files. # A default Rscons builder which knows how to produce an object file from
class Object < Builder # various types of source files.
def default_variables(env) class Object < Builder
{ KNOWN_SUFFIXES = {
'OBJSUFFIX' => '.o', "AS" => "ASSUFFIX",
"CC" => "CSUFFIX",
'AS' => '$CC', "CXX" => "CXXSUFFIX",
'ASFLAGS' => [], "DC" => "DSUFFIX",
'ASSUFFIX' => '.S',
'ASPPPATH' => '$CPPPATH',
'ASPPFLAGS' => '$CPPFLAGS',
'ASDEPGEN' => ['-MMD', '-MF', '$DEPFILE'],
'ASCOM' => ['$AS', '-c', '-o', '$TARGET', '$ASDEPGEN', '-I$[ASPPPATH]', '$ASPPFLAGS', '$ASFLAGS', '$SOURCES'],
'CPPFLAGS' => [],
'CPPPATH' => [],
'CC' => 'gcc',
'CFLAGS' => [],
'CSUFFIX' => '.c',
'CCDEPGEN' => ['-MMD', '-MF', '$DEPFILE'],
'CCCOM' => ['$CC', '-c', '-o', '$TARGET', '$CCDEPGEN', '-I$[CPPPATH]', '$CPPFLAGS', '$CFLAGS', '$SOURCES'],
'CXX' => 'g++',
'CXXFLAGS' => [],
'CXXSUFFIX' => '.cc',
'CXXDEPGEN' => ['-MMD', '-MF', '$DEPFILE'],
'CXXCOM' =>['$CXX', '-c', '-o', '$TARGET', '$CXXDEPGEN', '-I$[CPPPATH]', '$CPPFLAGS', '$CXXFLAGS', '$SOURCES'],
} }
end
def produces?(target, source, env) def default_variables(env)
target.has_suffix?(env['OBJSUFFIX']) and ( {
source.has_suffix?(env['ASSUFFIX']) or 'OBJSUFFIX' => '.o',
source.has_suffix?(env['CSUFFIX']) or
source.has_suffix?(env['CXXSUFFIX']))
end
def run(target, sources, cache, env) 'AS' => '${CC}',
vars = { 'ASFLAGS' => [],
'TARGET' => target, 'ASSUFFIX' => '.S',
'SOURCES' => sources, 'ASPPPATH' => '${CPPPATH}',
'DEPFILE' => target.set_suffix('.mf'), 'ASPPFLAGS' => '${CPPFLAGS}',
} 'ASDEPGEN' => ['-MMD', '-MF', '${_DEPFILE}'],
com_prefix = if sources.first.has_suffix?(env['ASSUFFIX']) 'ASCMD' => ['${AS}', '-c', '-o', '${_TARGET}', '${ASDEPGEN}', '-I${ASPPPATH}', '${ASPPFLAGS}', '${ASFLAGS}', '${_SOURCES}'],
'AS'
elsif sources.first.has_suffix?(env['CSUFFIX']) 'CPPFLAGS' => [],
'CC' 'CPPPATH' => [],
elsif sources.first.has_suffix?(env['CXXSUFFIX'])
'CXX' 'CC' => 'gcc',
else 'CFLAGS' => [],
raise "Error: unknown input file type: #{sources.first.inspect}" 'CSUFFIX' => '.c',
end 'CCDEPGEN' => ['-MMD', '-MF', '${_DEPFILE}'],
command = env.build_command(env["#{com_prefix}COM"], vars) 'CCCMD' => ['${CC}', '-c', '-o', '${_TARGET}', '${CCDEPGEN}', '-I${CPPPATH}', '${CPPFLAGS}', '${CFLAGS}', '${_SOURCES}'],
unless cache.up_to_date?(target, command, sources)
return false unless env.execute("#{com_prefix} #{target}", command) 'CXX' => 'g++',
deps = sources 'CXXFLAGS' => [],
if File.exists?(vars['DEPFILE']) 'CXXSUFFIX' => '.cc',
deps += env.parse_makefile_deps(vars['DEPFILE'], target) 'CXXDEPGEN' => ['-MMD', '-MF', '${_DEPFILE}'],
FileUtils.rm_f(vars['DEPFILE']) 'CXXCMD' =>['${CXX}', '-c', '-o', '${_TARGET}', '${CXXDEPGEN}', '-I${CPPPATH}', '${CPPFLAGS}', '${CXXFLAGS}', '${_SOURCES}'],
end
cache.register_build(target, command, deps.uniq) 'DC' => 'gdc',
'DFLAGS' => [],
'DSUFFIX' => '.d',
'D_IMPORT_PATH' => [],
'DCCMD' => ['${DC}', '-c', '-o', '${_TARGET}', '-I${D_IMPORT_PATH}', '${DFLAGS}', '${_SOURCES}'],
}
end
def produces?(target, source, env)
target.end_with?(*env['OBJSUFFIX']) and KNOWN_SUFFIXES.find do |compiler, suffix_var|
source.end_with?(*env[suffix_var])
end
end
def run(target, sources, cache, env, vars)
vars = vars.merge({
'_TARGET' => target,
'_SOURCES' => sources,
'_DEPFILE' => Rscons.set_suffix(target, '.mf'),
})
com_prefix = KNOWN_SUFFIXES.find do |compiler, suffix_var|
sources.first.end_with?(*env[suffix_var])
end.tap do |v|
v.nil? and raise "Error: unknown input file type: #{sources.first.inspect}"
end.first
command = env.build_command(env["#{com_prefix}CMD"], vars)
unless cache.up_to_date?(target, command, sources, env)
cache.mkdir_p(File.dirname(target))
FileUtils.rm_f(target)
return false unless env.execute("#{com_prefix} #{target}", command)
deps = sources
if File.exists?(vars['_DEPFILE'])
deps += Environment.parse_makefile_deps(vars['_DEPFILE'], target)
FileUtils.rm_f(vars['_DEPFILE'])
end
cache.register_build(target, command, deps.uniq, env)
end
target
end end
target
end end
end end
end end
+25
View File
@@ -0,0 +1,25 @@
module Rscons
module Builders
# The Preprocess builder invokes the C preprocessor
class Preprocess < Builder
def default_variables(env)
{
"CPP_CMD" => ["${_PREPROCESS_CC}", "-E", "-o", "${_TARGET}", "-I${CPPPATH}", "${CPPFLAGS}", "${CFLAGS}", "${_SOURCES}"],
}
end
def run(target, sources, cache, env, vars)
pp_cc = if sources.find {|s| s.end_with?(*env["CXXSUFFIX"])}
env["CXX"]
else
env["CC"]
end
vars = vars.merge("_PREPROCESS_CC" => pp_cc,
"_TARGET" => target,
"_SOURCES" => sources)
command = env.build_command(env["CPP_CMD"], vars)
standard_build("Preprocess #{target}", target, command, sources, env, cache)
end
end
end
end
+35 -42
View File
@@ -1,47 +1,40 @@
module Rscons module Rscons
# A default RScons builder that knows how to link object files into an module Builders
# executable program. # A default Rscons builder that knows how to link object files into an
class Program < Builder # executable program.
def default_variables(env) class Program < Builder
{ def default_variables(env)
'LD' => nil, {
'OBJSUFFIX' => '.o', 'LD' => nil,
'LIBSUFFIX' => '.a', 'OBJSUFFIX' => '.o',
'LDFLAGS' => [], 'LIBSUFFIX' => '.a',
'LIBPATHS' => [], 'LDFLAGS' => [],
'LIBS' => [], 'LIBPATH' => [],
'LDCOM' => ['$LD', '-o', '$TARGET', '$LDFLAGS', '$SOURCES', '-L$[LIBPATHS]', '-l$[LIBS]'] 'LIBS' => [],
} 'LDCMD' => ['${LD}', '-o', '${_TARGET}', '${LDFLAGS}', '${_SOURCES}', '-L${LIBPATH}', '-l${LIBS}']
end
def run(target, sources, cache, env)
# convert sources to object file names
objects = sources.map do |source|
if source.has_suffix?([env['OBJSUFFIX'], env['LIBSUFFIX']])
source
else
o_file = env.get_build_fname(source, env['OBJSUFFIX', :string])
builder = env.builders.values.find { |b| b.produces?(o_file, source, env) }
builder or raise "No builder found to convert input source #{source.inspect} to an object file."
builder.run(o_file, [source], cache, env) or break
end
end
if objects
use_cxx = sources.map do |s|
s.has_suffix?(env['CXXSUFFIX'])
end.any?
ld_alt = use_cxx ? env['CXX'] : env['CC']
vars = {
'TARGET' => target,
'SOURCES' => objects,
'LD' => env['LD'] || ld_alt,
} }
command = env.build_command(env['LDCOM'], vars) end
unless cache.up_to_date?(target, command, objects)
return false unless env.execute("LD #{target}", command) def run(target, sources, cache, env, vars)
cache.register_build(target, command, objects) # build sources to linkable objects
end objects = env.build_sources(sources, [env['OBJSUFFIX'], env['LIBSUFFIX']].flatten, cache, vars)
target return false unless objects
ld = if env["LD"]
env["LD"]
elsif sources.find {|s| s.end_with?(*env["DSUFFIX"])}
env["DC"]
elsif sources.find {|s| s.end_with?(*env["CXXSUFFIX"])}
env["CXX"]
else
env["CC"]
end
vars = vars.merge({
'_TARGET' => target,
'_SOURCES' => objects,
'LD' => ld,
})
command = env.build_command(env['LDCMD'], vars)
standard_build("LD #{target}", target, command, objects, env, cache)
end end
end end
end end
+167 -86
View File
@@ -1,136 +1,217 @@
require 'yaml' require "digest/md5"
require 'fileutils' require "fileutils"
require 'digest/md5' require "json"
require 'set' require "set"
require 'rscons/version' require "singleton"
require "rscons/version"
module Rscons module Rscons
# The Cache class keeps track of file checksums, build target commands and # The Cache class keeps track of file checksums, build target commands and
# dependencies in a YAML file which persists from one invocation to the next. # dependencies in a JSON file which persists from one invocation to the next.
# Example cache: # Example cache:
# { # {
# version: '1.2.3', # "version" => "1.2.3",
# targets: { # "targets" => {
# 'program' => { # "program" => {
# 'checksum' => 'A1B2C3D4', # "checksum" => "A1B2C3D4",
# 'command' => ['gcc', '-o', 'program', 'program.o'], # "command" => "13543518FE",
# 'deps' => [ # "deps" => [
# { # {
# 'fname' => 'program.o', # "fname" => "program.o",
# 'checksum' => '87654321', # "checksum" => "87654321",
# } # },
# ], # ],
# } # "user_deps" => [
# 'program.o' => {
# 'checksum' => '87654321',
# 'command' => ['gcc', '-c', '-o', 'program.o', 'program.c'],
# 'deps' => [
# { # {
# 'fname' => 'program.c', # "fname" => "lscript.ld",
# 'checksum' => '456789ABC', # "checksum" => "77551133",
# },
# ],
# },
# "program.o" => {
# "checksum" => "87654321",
# "command" => "98765ABCD",
# "deps" => [
# {
# "fname" => "program.c",
# "checksum" => "456789ABC",
# }, # },
# { # {
# 'fname' => 'program.h', # "fname" => "program.h",
# 'checksum' => '7979764643', # "checksum" => "7979764643",
# } # },
# ] # ],
# "user_deps" => [],
# } # }
# } # },
# "directories" => {
# "build" => true,
# "build/one" => true,
# "build/two" => true,
# },
# } # }
class Cache class Cache
#### Constants include Singleton
# Name of the file to store cache information in # Name of the file to store cache information in
CACHE_FILE = '.rsconscache' CACHE_FILE = ".rsconscache"
#### Class Methods
# Remove the cache file
def self.clear
FileUtils.rm_f(CACHE_FILE)
end
#### Instance Methods
# Create a Cache object and load in the previous contents from the cache # Create a Cache object and load in the previous contents from the cache
# file. # file.
def initialize def initialize
@cache = YAML.load(File.read(CACHE_FILE)) rescue { initialize!
targets: {}, end
version: VERSION,
} # Remove the cache file.
def clear
FileUtils.rm_f(CACHE_FILE)
initialize!
end
# Clear the cached file checksums.
def clear_checksum_cache!
@lookup_checksums = {} @lookup_checksums = {}
end end
# Write the cache to disk to be loaded next time. # Write the cache to disk to be loaded next time.
def write def write
File.open(CACHE_FILE, 'w') do |fh| if @dirty || (@cache["version"] != VERSION)
fh.puts(YAML.dump(@cache)) @cache["version"] = VERSION
File.open(CACHE_FILE, "w") do |fh|
fh.puts(JSON.dump(@cache))
end
end end
@dirty = false
end end
# Check if a target is up to date # Check if target(s) are up to date
# @param target [String] The name of the target file. # @param targets [String, Array] The name(s) of the target file(s).
# @param command [Array] The command used to build the target. # @param command [String, Array] The command used to build the target.
# @param deps [Array] List of the target's dependency files. # @param deps [Array] List of the target's dependency files.
# @param options [Hash] Optional options. Can contain the following keys: # @param env [Environment] The Rscons::Environment.
# :strict_deps:: # @param options [Hash] Optional options.
# Only consider a target up to date if its list of dependencies is # @option options [Boolean] :strict_deps
# exactly equal (including order) to the cached list of dependencies # Only consider a target up to date if its list of dependencies is
# @return true value if the target is up to date, meaning that: # exactly equal (including order) to the cached list of dependencies
# @return [Boolean]
# True value if the targets are all up to date, meaning that,
# for each target:
# - the target exists on disk # - the target exists on disk
# - the cache has information for the target # - the cache has information for the target
# - the target's checksum matches its checksum when it was last built
# - the command used to build the target is the same as last time # - the command used to build the target is the same as last time
# - all dependencies listed are also listed in the cache, or, if # - all dependencies listed are also listed in the cache, or, if
# :strict_deps was given in options, the list of dependencies is # :strict_deps was given in options, the list of dependencies is
# exactly equal to those cached # exactly equal to those cached
# - each cached dependency file's current checksum matches the checksum # - each cached dependency file's current checksum matches the checksum
# stored in the cache file # stored in the cache file
def up_to_date?(target, command, deps, options = {}) def up_to_date?(targets, command, deps, env, options = {})
# target file must exist on disk Array(targets).each do |target|
return false unless File.exists?(target) # target file must exist on disk
return false unless File.exists?(target)
# target must be registered in the cache # target must be registered in the cache
return false unless @cache[:targets].has_key?(target) return false unless @cache["targets"].has_key?(target)
# command used to build target must be identical # target must have the same checksum as when it was built last
return false unless @cache[:targets][target][:command] == command return false unless @cache["targets"][target]["checksum"] == lookup_checksum(target)
cached_deps = @cache[:targets][target][:deps].map { |dc| dc[:fname] } # command used to build target must be identical
if options[:strict_deps] return false unless @cache["targets"][target]["command"] == Digest::MD5.hexdigest(command.inspect)
# depedencies passed in must exactly equal those in the cache
return false unless deps == cached_deps cached_deps = @cache["targets"][target]["deps"] || []
else cached_deps_fnames = cached_deps.map { |dc| dc["fname"] }
# all dependencies passed in must exist in cache (but cache may have more) if options[:strict_deps]
return false unless (Set.new(deps) - Set.new(cached_deps)).empty? # depedencies passed in must exactly equal those in the cache
return false unless deps == cached_deps_fnames
else
# all dependencies passed in must exist in cache (but cache may have more)
return false unless (Set.new(deps) - Set.new(cached_deps_fnames)).empty?
end
# set of user dependencies must match
user_deps = env.get_user_deps(target) || []
cached_user_deps = @cache["targets"][target]["user_deps"] || []
cached_user_deps_fnames = cached_user_deps.map { |dc| dc["fname"] }
return false unless user_deps == cached_user_deps_fnames
# all cached dependencies must have their checksums match
(cached_deps + cached_user_deps).each do |dep_cache|
return false unless dep_cache["checksum"] == lookup_checksum(dep_cache["fname"])
end
end end
# all cached dependencies must have their checksums match true
@cache[:targets][target][:deps].map do |dep_cache|
dep_cache[:checksum] == lookup_checksum(dep_cache[:fname])
end.all?
end end
# Store cache information about a target built by a builder # Store cache information about target(s) built by a builder
# @param target [String] The name of the target. # @param targets [String, Array] The name of the target(s) built.
# @param command [Array] The command used to build the target. # @param command [String, Array] The command used to build the target.
# @param deps [Array] List of dependencies for the target. # @param deps [Array] List of dependencies for the target.
def register_build(target, command, deps) # @param env [Environment] The {Rscons::Environment}.
@cache[:targets][target] = { def register_build(targets, command, deps, env)
command: command, Array(targets).each do |target|
checksum: calculate_checksum(target), @cache["targets"][target] = {
deps: deps.map do |dep| "command" => Digest::MD5.hexdigest(command.inspect),
{ "checksum" => calculate_checksum(target),
fname: dep, "deps" => deps.map do |dep|
checksum: lookup_checksum(dep), {
} "fname" => dep,
end "checksum" => lookup_checksum(dep),
} }
end,
"user_deps" => (env.get_user_deps(target) || []).map do |dep|
{
"fname" => dep,
"checksum" => lookup_checksum(dep),
}
end,
}
@dirty = true
end
end
# Return a list of targets that have been built
def targets
@cache["targets"].keys
end
# Make any needed directories and record the ones that are created for
# removal upon a "clean" operation.
def mkdir_p(path)
parts = path.split(/[\\\/]/)
parts.each_index do |i|
next if parts[i] == ""
subpath = File.join(*parts[0, i + 1])
unless File.exists?(subpath)
FileUtils.mkdir(subpath)
@cache["directories"][subpath] = true
@dirty = true
end
end
end
# Return a list of directories which were created as a part of the build
def directories
@cache["directories"].keys
end end
# Private Instance Methods
private private
# Create a Cache object and load in the previous contents from the cache
# file.
def initialize!
@cache = JSON.load(File.read(CACHE_FILE)) rescue {}
unless @cache.is_a?(Hash)
$stderr.puts "Warning: #{CACHE_FILE} was corrupt. Contents:\n#{@cache.inspect}"
@cache = {}
end
@cache["targets"] ||= {}
@cache["directories"] ||= {}
@lookup_checksums = {}
@dirty = false
end
# Return a file's checksum, or the previously calculated checksum for # Return a file's checksum, or the previously calculated checksum for
# the same file # the same file
# @param file [String] The file name. # @param file [String] The file name.
@@ -141,7 +222,7 @@ module Rscons
# Calculate and return a file's checksum # Calculate and return a file's checksum
# @param file [String] The file name. # @param file [String] The file name.
def calculate_checksum(file) def calculate_checksum(file)
@lookup_checksums[file] = Digest::MD5.hexdigest(File.read(file)).encode(__ENCODING__) rescue '' @lookup_checksums[file] = Digest::MD5.hexdigest(File.read(file, mode: "rb")) rescue ""
end end
end end
end end
+261 -92
View File
@@ -2,38 +2,47 @@ require 'set'
require 'fileutils' require 'fileutils'
module Rscons module Rscons
# The Environment class is the main programmatic interface to RScons. It # The Environment class is the main programmatic interface to Rscons. It
# contains a collection of construction variables, options, builders, and # contains a collection of construction variables, options, builders, and
# rules for building targets. # rules for building targets.
class Environment class Environment
# [Array] of {Builder} objects. # Hash of +{"builder_name" => builder_object}+ pairs.
attr_reader :builders attr_reader :builders
# :command, :short, or :off
attr_accessor :echo
# String or +nil+
attr_reader :build_root
def build_root=(build_root)
@build_root = build_root
@build_root.gsub!('\\', '/') if @build_root
end
# Create an Environment object. # Create an Environment object.
# @param variables [Hash] # @param options [Hash]
# The variables hash can contain both construction variables, which are # Possible options keys:
# uppercase strings (such as "CC" or "LDFLAGS"), and RScons options, # :echo => :command, :short, or :off (default :short)
# which are lowercase symbols (such as :echo). # :build_root => String specifying build root directory (default nil)
# :exclude_builders => true to omit adding default builders (default false)
# If a block is given, the Environment object is yielded to the block and # If a block is given, the Environment object is yielded to the block and
# when the block returns, the {#process} method is automatically called. # when the block returns, the {#process} method is automatically called.
def initialize(variables = {}) def initialize(options = {})
@varset = VarSet.new(variables) @varset = VarSet.new
@targets = {} @targets = {}
@user_deps = {}
@builders = {} @builders = {}
@build_dirs = {} @build_dirs = []
@varset[:exclude_builders] ||= [] @build_hooks = []
unless @varset[:exclude_builders] == :all unless options[:exclude_builders]
exclude_builders = Set.new(@varset[:exclude_builders] || []) DEFAULT_BUILDERS.each do |builder_class_name|
DEFAULT_BUILDERS.each do |builder_class| builder_class = Builders.const_get(builder_class_name)
unless exclude_builders.include?(builder_class.short_name) builder_class or raise "Could not find builder class #{builder_class_name}"
add_builder(builder_class.new) add_builder(builder_class.new)
end
end end
end end
(@varset[:builders] || []).each do |builder| @echo = options[:echo] || :short
add_builder(builder) @build_root = options[:build_root]
end
@varset[:echo] ||= :command
if block_given? if block_given?
yield self yield self
@@ -42,21 +51,53 @@ module Rscons
end end
# Make a copy of the Environment object. # Make a copy of the Environment object.
# The cloned environment will contain a copy of all environment options, #
# construction variables, builders, and build directories. It will not # By default, a cloned environment will contain a copy of all environment
# contain a copy of the targets. # options, construction variables, and builders, but not a copy of the
# targets, build hooks, build directories, or the build root.
#
# Exactly which items are cloned are controllable via the optional :clone
# parameter, which can be :none, :all, or a set or array of any of the
# following:
# - :variables to clone construction variables (on by default)
# - :builders to clone the builders (on by default)
# - :build_root to clone the build root (off by default)
# - :build_dirs to clone the build directories (off by default)
# - :build_hooks to clone the build hooks (off by default)
#
# If a block is given, the Environment object is yielded to the block and # If a block is given, the Environment object is yielded to the block and
# when the block returns, the {#process} method is automatically called. # when the block returns, the {#process} method is automatically called.
def clone(variables = {}) #
env = Environment.new() # Any options that #initialize receives can also be specified here.
@builders.each do |builder_name, builder| #
env.add_builder(builder) # @return a new {Environment} object.
def clone(options = {})
clone = options[:clone] || Set[:variables, :builders]
clone = Set[:variables, :builders, :build_root, :build_dirs, :build_hooks] if clone == :all
clone = Set[] if clone == :none
clone = Set.new(clone) if clone.is_a?(Array)
clone.delete(:builders) if options[:exclude_builders]
env = self.class.new(
echo: options[:echo] || @echo,
build_root: options[:build_root],
exclude_builders: true)
if clone.include?(:builders)
@builders.each do |builder_name, builder|
env.add_builder(builder)
end
end end
@build_dirs.each do |src_dir, obj_dir| env.append(@varset) if clone.include?(:variables)
env.build_dir(src_dir, obj_dir) env.build_root = @build_root if clone.include?(:build_root)
if clone.include?(:build_dirs)
@build_dirs.each do |src_dir, obj_dir|
env.build_dir(src_dir, obj_dir)
end
end
if clone.include?(:build_hooks)
@build_hooks.each do |build_hook_block|
env.add_build_hook(&build_hook_block)
end
end end
env.append(@varset)
env.append(variables)
if block_given? if block_given?
yield env yield env
@@ -67,7 +108,7 @@ module Rscons
# Add a {Builder} object to the Environment. # Add a {Builder} object to the Environment.
def add_builder(builder) def add_builder(builder)
@builders[builder.class.short_name] = builder @builders[builder.name] = builder
var_defs = builder.default_variables(self) var_defs = builder.default_variables(self)
if var_defs if var_defs
var_defs.each_pair do |var, val| var_defs.each_pair do |var, val|
@@ -76,22 +117,35 @@ module Rscons
end end
end end
# Add a build hook to the Environment.
def add_build_hook(&block)
@build_hooks << block
end
# Specify a build directory for this Environment. # Specify a build directory for this Environment.
# Source files from src_dir will produce object files under obj_dir. # Source files from src_dir will produce object files under obj_dir.
def build_dir(src_dir, obj_dir) def build_dir(src_dir, obj_dir)
@build_dirs[src_dir.gsub('\\', '/')] = obj_dir.gsub('\\', '/') src_dir = src_dir.gsub('\\', '/') if src_dir.is_a?(String)
@build_dirs << [src_dir, obj_dir]
end end
# Return the file name to be built from source_fname with suffix suffix. # Return the file name to be built from source_fname with suffix suffix.
# This method takes into account the Environment's build directories. # This method takes into account the Environment's build directories.
# It also creates any parent directories needed to be able to open and
# write to the output file.
def get_build_fname(source_fname, suffix) def get_build_fname(source_fname, suffix)
build_fname = source_fname.set_suffix(suffix).gsub('\\', '/') build_fname = Rscons.set_suffix(source_fname, suffix).gsub('\\', '/')
@build_dirs.each do |src_dir, obj_dir| found_match = @build_dirs.find do |src_dir, obj_dir|
build_fname.sub!(/^#{src_dir}\//, "#{obj_dir}/") if src_dir.is_a?(Regexp)
build_fname.sub!(src_dir, obj_dir)
else
build_fname.sub!(%r{^#{src_dir}/}, "#{obj_dir}/")
end
end end
FileUtils.mkdir_p(File.dirname(build_fname)) if @build_root and not found_match
unless Rscons.absolute_path?(source_fname) or build_fname.start_with?("#{@build_root}/")
build_fname = "#{@build_root}/#{build_fname}"
end
end
build_fname.gsub!('\\', '/')
build_fname build_fname
end end
@@ -110,46 +164,49 @@ module Rscons
# Add a set of construction variables or environment options. # Add a set of construction variables or environment options.
# @see VarSet#append # @see VarSet#append
def append(*args) def append(*args)
@varset.send(:append, *args) @varset.append(*args)
end
# Return a list of target file names
def targets
@targets.keys
end
# Return a list of sources needed to build target target.
def target_sources(target)
@targets[target][:source] rescue nil
end end
# Build all target specified in the Environment. # Build all target specified in the Environment.
# When a block is passed to Environment.new, this method is automatically # When a block is passed to Environment.new, this method is automatically
# called after the block returns. # called after the block returns.
def process def process
cache = Cache.new unless @targets.empty?
targets_processed = Set.new expand_paths!
process_target = proc do |target| cache = Cache.instance
if @targets[target][:source].map do |src| cache.clear_checksum_cache!
targets_processed.include?(src) or not @targets.include?(src) or process_target.call(src) targets_processed = {}
end.all? process_target = proc do |target|
@targets[target][:builder].run(target, targets_processed[target] ||= begin
@targets[target][:source], @targets[target][:sources].each do |src|
cache, if @targets.include?(src) and not targets_processed.include?(src)
self, process_target.call(src)
*@targets[target][:args]) end
else end
false result = run_builder(@targets[target][:builder],
target,
@targets[target][:sources],
cache,
@targets[target][:vars] || {})
unless result
raise BuildError.new("Failed to build #{target}")
end
result
end
end
begin
@targets.each do |target, target_params|
process_target.call(target)
end
ensure
cache.write
end end
end end
@targets.each do |target, info| end
next if targets_processed.include?(target)
unless process_target.call(target) # Clear all targets registered for the Environment.
$stderr.puts "Error: failed to build #{target}" def clear_targets
break @targets = {}
end
end
cache.write
end end
# Build a command line from the given template, resolving references to # Build a command line from the given template, resolving references to
@@ -164,58 +221,170 @@ module Rscons
@varset.merge(extra_vars).expand_varref(command_template) @varset.merge(extra_vars).expand_varref(command_template)
end end
# Expand a construction variable reference (String or Array)
def expand_varref(varref)
@varset.expand_varref(varref)
end
# Execute a builder command # Execute a builder command
# @param short_desc [String] Message to print if the Environment's :echo # @param short_desc [String] Message to print if the Environment's echo
# mode is set to :short # mode is set to :short
# @param command [Array] The command to execute. # @param command [Array] The command to execute.
# @param options [Hash] Optional options to pass to {Kernel#system}. # @param options [Hash] Optional options, possible keys:
# - :env - environment Hash to pass to Kernel#system.
# - :options - options Hash to pass to Kernel#system.
def execute(short_desc, command, options = {}) def execute(short_desc, command, options = {})
print_command = proc do print_command = proc do
puts command.map { |c| c =~ /\s/ ? "'#{c}'" : c }.join(' ') puts command.map { |c| c =~ /\s/ ? "'#{c}'" : c }.join(' ')
end end
if @varset[:echo] == :command if @echo == :command
print_command.call print_command.call
elsif @varset[:echo] == :short elsif @echo == :short
puts short_desc puts short_desc
end end
system(*command, options).tap do |result| env_args = options[:env] ? [options[:env]] : []
unless result or @varset[:echo] == :command options_args = options[:options] ? [options[:options]] : []
system(*env_args, *command, *options_args).tap do |result|
unless result or @echo == :command
$stdout.write "Failed command was: " $stdout.write "Failed command was: "
print_command.call print_command.call
end end
end end
end end
alias_method :orig_method_missing, :method_missing
def method_missing(method, *args) def method_missing(method, *args)
if @builders.has_key?(method.to_s) if @builders.has_key?(method.to_s)
target, source, *rest = args target, sources, vars, *rest = args
source = [source] unless source.is_a?(Array) unless vars.nil? or vars.is_a?(Hash) or vars.is_a?(VarSet)
@targets[target] = { raise "Unexpected construction variable set: #{vars.inspect}"
builder: @builders[method.to_s], end
source: source, sources = Array(sources)
args: rest, add_target(target, @builders[method.to_s], sources, vars, rest)
}
else else
orig_method_missing(method, *args) super
end
end
def add_target(target, builder, sources, vars, args)
@targets[target] = {
builder: builder,
sources: sources,
vars: vars,
args: args,
}
end
# Manually record a given target as depending on the specified
# dependency files.
def depends(target, *user_deps)
@user_deps[target] ||= []
@user_deps[target] = (@user_deps[target] + user_deps).uniq
end
# Return the list of user dependencies for a given target, or +nil+ for
# none.
def get_user_deps(target)
@user_deps[target]
end
# Build a list of source files into files containing one of the suffixes
# given by suffixes.
# This method is used internally by Rscons builders.
# @param sources [Array] List of source files to build.
# @param suffixes [Array] List of suffixes to try to convert source files into.
# @param cache [Cache] The Cache.
# @param vars [Hash] Extra variables to pass to the builder.
# Return a list of the converted file names.
def build_sources(sources, suffixes, cache, vars)
sources.map do |source|
if source.end_with?(*suffixes)
source
else
converted = nil
suffixes.each do |suffix|
converted_fname = get_build_fname(source, suffix)
builder = @builders.values.find { |b| b.produces?(converted_fname, source, self) }
if builder
converted = run_builder(builder, converted_fname, [source], cache, vars)
return nil unless converted
break
end
end
converted or raise "Could not find a builder to handle #{source.inspect}."
end
end
end
# Invoke a builder to build the given target based on the given sources.
# @param builder [Builder] The Builder to use.
# @param target [String] The target output file.
# @param sources [Array] List of source files.
# @param cache [Cache] The Cache.
# @param vars [Hash] Extra variables to pass to the builder.
# Return the result of the builder's run() method.
def run_builder(builder, target, sources, cache, vars)
vars = @varset.merge(vars)
@build_hooks.each do |build_hook_block|
build_operation = {
builder: builder,
target: target,
sources: sources,
vars: vars,
env: self,
}
build_hook_block.call(build_operation)
end
builder.run(target, sources, cache, self, vars)
end
# Expand a path to be relative to the Environment's build root.
#
# Paths beginning with "^/" are expanded by replacing "^" with the
# Environment's build root.
#
# @param path [String] The path to expand.
#
# @return [String] The expanded path.
def expand_path(path)
path.sub(%r{^\^(?=[\\/])}, @build_root)
end
private
# Expand target and source paths before invoking builders.
#
# This method expand construction variable references in the target and
# source file names before passing them to the builder. It also expands
# "^/" prefixes to the Environment's build root if a build root is defined.
def expand_paths!
@targets = @targets.reduce({}) do |result, (target, target_params)|
sources = target_params[:sources].map do |source|
source = expand_path(source) if @build_root
expand_varref(source)
end
target = expand_path(target) if @build_root
target = expand_varref(target)
result[target] = target_params.merge(sources: sources)
result
end end
end end
# Parse dependencies for a given target from a Makefile. # Parse dependencies for a given target from a Makefile.
# This method is used internally by RScons builders. # This method is used internally by Rscons builders.
# @param mf_fname [String] File name of the Makefile to read. # @param mf_fname [String] File name of the Makefile to read.
# @param target [String] Name of the target to gather dependencies for. # @param target [String] Name of the target to gather dependencies for.
def parse_makefile_deps(mf_fname, target) def self.parse_makefile_deps(mf_fname, target)
deps = [] deps = []
buildup = '' buildup = ''
File.read(mf_fname).each_line do |line| File.read(mf_fname).each_line do |line|
if line =~ /^(.*)\\\s*$/ if line =~ /^(.*)\\\s*$/
buildup += ' ' + $1 buildup += ' ' + $1
else else
if line =~ /^(.*): (.*)$/ buildup += ' ' + line
target, tdeps = $1.strip, $2 if buildup =~ /^(.*): (.*)$/
if target == target mf_target, mf_deps = $1.strip, $2
deps += tdeps.split(' ').map(&:strip) if mf_target == target
deps += mf_deps.split(' ').map(&:strip)
end end
end end
buildup = '' buildup = ''
-7
View File
@@ -1,7 +0,0 @@
# Standard Ruby Module class.
class Module
# @return the base module name (not the fully qualified name)
def short_name
name.split(':').last
end
end
-19
View File
@@ -1,19 +0,0 @@
# Standard Ruby String class.
class String
# Check if the given string ends with any of the supplied suffixes
# @param suffix [String, Array] The suffix to look for.
# @return a true value if the string ends with one of the suffixes given.
def has_suffix?(suffix)
if suffix
suffix = [suffix] if suffix.is_a?(String)
suffix.find {|s| self.end_with?(s)}
end
end
# Return a new string with the suffix (dot character and extension) changed
# to the given suffix.
# @param suffix [String] The new suffix.
def set_suffix(suffix = '')
sub(/\.[^.]*$/, suffix)
end
end
+80 -35
View File
@@ -1,53 +1,73 @@
module Rscons module Rscons
# This class represents a collection of variables which can be accessed # This class represents a collection of variables which can be accessed
# as certain types # as certain types.
# Only nil, strings, arrays, and hashes should be stored in a VarSet.
class VarSet class VarSet
# The underlying hash # Create a VarSet.
attr_reader :vars
# Create a VarSet
# @param vars [Hash] Optional initial variables. # @param vars [Hash] Optional initial variables.
def initialize(vars = {}) def initialize(vars = {})
@vars = vars @my_vars = {}
@coa_vars = []
append(vars)
end end
# Access the value of variable as a particular type # Access the value of variable as a particular type
# @param key [String, Symbol] The variable name. # @param key [String, Symbol] The variable name.
# @param type [Symbol, nil] Optional specification of the type desired. # @return [Object] The variable's value.
# If the variable is a String and type is :array, a 1-element array with def [](key)
# the variable value will be returned. If the variable is an Array and if @my_vars.include?(key)
# type is :string, the first element from the variable value will be @my_vars[key]
# returned.
def [](key, type = nil)
val = @vars[key]
if type == :array and val.is_a?(String)
[val]
elsif type == :string and val.is_a?(Array)
val.first
else else
val @coa_vars.each do |coa_vars|
if coa_vars.include?(key)
@my_vars[key] = deep_dup(coa_vars[key])
return @my_vars[key]
end
end
nil
end end
end end
# Assign a value to a variable. # Assign a value to a variable.
# @param key [String, Symbol] The variable name. # @param key [String, Symbol] The variable name.
# @param val [Object] The value. # @param val [Object] The value to set.
def []=(key, val) def []=(key, val)
@vars[key] = val @my_vars[key] = val
end
# Check if the VarSet contains a variable.
# @param key [String, Symbol] The variable name.
# @return [true, false] Whether the VarSet contains a variable.
def include?(key)
if @my_vars.include?(key)
true
else
@coa_vars.find do |coa_vars|
coa_vars.include?(key)
end
end
end end
# Add or overwrite a set of variables # Add or overwrite a set of variables
# @param values [VarSet, Hash] New set of variables. # @param values [VarSet, Hash] New set of variables.
def append(values) def append(values)
values = values.vars if values.is_a?(VarSet) coa!
@vars.merge!(values) if values.is_a?(VarSet)
values.send(:coa!)
@coa_vars = values.instance_variable_get(:@coa_vars) + @coa_vars
else
@my_vars = deep_dup(values)
end
self self
end end
# Create a new VarSet object based on the first merged with other. # Create a new VarSet object based on the first merged with other.
# @param other [VarSet, Hash] Other variables to add or overwrite. # @param other [VarSet, Hash] Other variables to add or overwrite.
def merge(other = {}) def merge(other = {})
VarSet.new(Marshal.load(Marshal.dump(@vars))).append(other) coa!
varset = self.class.new
varset.instance_variable_set(:@coa_vars, @coa_vars.dup)
varset.append(other)
end end
alias_method :clone, :merge alias_method :clone, :merge
@@ -60,24 +80,49 @@ module Rscons
expand_varref(ent) expand_varref(ent)
end.flatten end.flatten
else else
if varref =~ /^(.*)\$\[(\w+)\](.*)$/ if varref =~ /^(.*)\$\{([^}]+)\}(.*)$/
# expand array with given prefix, suffix
prefix, varname, suffix = $1, $2, $3 prefix, varname, suffix = $1, $2, $3
varval = expand_varref(@vars[varname]) varval = expand_varref(self[varname])
unless varval.is_a?(Array) if varval.is_a?(String)
raise "Array expected for $#{varname}" expand_varref("#{prefix}#{varval}#{suffix}")
elsif varval.is_a?(Array)
varval.map {|vv| expand_varref("#{prefix}#{vv}#{suffix}")}.flatten
else
raise "I do not know how to expand a variable reference to a #{varval.class.name} (from #{varname.inspect} => #{self[varname].inspect})"
end end
varval.map {|e| "#{prefix}#{e}#{suffix}"}
elsif varref =~ /^\$(.*)$/
# expand a single variable reference
varname = $1
varval = expand_varref(@vars[varname])
varval or raise "Could not find variable #{varname.inspect}"
expand_varref(varval)
else else
varref varref
end end
end end
end end
private
# Move all VarSet variables into the copy-on-access list.
def coa!
unless @my_vars.empty?
@coa_vars.unshift(@my_vars)
@my_vars = {}
end
end
# Create a deep copy of an object.
# @param obj [nil, String, Array, Hash] Object to deep copy.
# @return [nil, String, Array, Hash] Deep copied value.
def deep_dup(obj)
obj_class = obj.class
if obj_class == Hash
obj.reduce({}) do |result, (k, v)|
result[k] = deep_dup(v)
result
end
elsif obj_class == Array
obj.map { |v| deep_dup(v) }
elsif obj_class == String
obj.dup
else
obj
end
end
end end
end end
+1 -1
View File
@@ -1,4 +1,4 @@
module Rscons module Rscons
# gem version # gem version
VERSION = "0.0.1" VERSION = "1.4.2"
end end
+2 -1
View File
@@ -10,7 +10,8 @@ Gem::Specification.new do |gem|
gem.email = ["jholtrop@gmail.com"] gem.email = ["jholtrop@gmail.com"]
gem.description = %q{Software construction library inspired by SCons and implemented in Ruby} gem.description = %q{Software construction library inspired by SCons and implemented in Ruby}
gem.summary = %q{Software construction library inspired by SCons and implemented in Ruby} gem.summary = %q{Software construction library inspired by SCons and implemented in Ruby}
gem.homepage = "" gem.homepage = "https://github.com/holtrop/rscons"
gem.license = 'MIT'
gem.files = `git ls-files`.split($/) gem.files = `git ls-files`.split($/)
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) } gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
+410 -29
View File
@@ -1,27 +1,40 @@
require 'fileutils' require 'fileutils'
class Dir
class << self
alias_method :orig_bracket, :[]
end
def self.[](*args)
orig_bracket(*args).sort
end
end
describe Rscons do describe Rscons do
BUILD_TEST_RUN_DIR = "build_test_run"
before(:all) do before(:all) do
FileUtils.rm_rf('build_tests_run') FileUtils.rm_rf(BUILD_TEST_RUN_DIR)
@owd = Dir.pwd @owd = Dir.pwd
end end
before(:each) do
@output = ""
$stdout.stub(:write) do |content|
@output += content
end
$stderr.stub(:write) do |content|
@output += content
end
end
after(:each) do after(:each) do
Dir.chdir(@owd) Dir.chdir(@owd)
FileUtils.rm_rf('build_tests_run') FileUtils.rm_rf(BUILD_TEST_RUN_DIR)
end
def build_testdir
if File.exists?("build.rb")
system("ruby -I #{@owd}/lib -r rscons build.rb > build.out")
end
get_build_output
end end
def test_dir(build_test_directory) def test_dir(build_test_directory)
FileUtils.cp_r("build_tests/#{build_test_directory}", 'build_tests_run') FileUtils.cp_r("build_tests/#{build_test_directory}", BUILD_TEST_RUN_DIR)
Dir.chdir("build_tests_run") Dir.chdir(BUILD_TEST_RUN_DIR)
build_testdir
end end
def file_sub(fname) def file_sub(fname)
@@ -35,8 +48,10 @@ describe Rscons do
end end
end end
def get_build_output def lines
File.read('build.out').lines.map(&:strip) @output.lines.map(&:rstrip).tap do |v|
@output = ""
end
end end
########################################################################### ###########################################################################
@@ -45,12 +60,19 @@ describe Rscons do
it 'builds a C program with one source file' do it 'builds a C program with one source file' do
test_dir('simple') test_dir('simple')
Rscons::Environment.new do |env|
env.Program('simple', Dir['*.c'])
end
File.exists?('simple.o').should be_true File.exists?('simple.o').should be_true
`./simple`.should == "This is a simple C program\n" `./simple`.should == "This is a simple C program\n"
end end
it 'prints commands as they are executed' do it 'prints commands as they are executed' do
lines = test_dir('simple') test_dir('simple')
Rscons::Environment.new(echo: :command) do |env|
env["LD"] = "gcc"
env.Program('simple', Dir['*.c'])
end
lines.should == [ lines.should == [
'gcc -c -o simple.o -MMD -MF simple.mf simple.c', 'gcc -c -o simple.o -MMD -MF simple.mf simple.c',
'gcc -o simple simple.o', 'gcc -o simple simple.o',
@@ -58,7 +80,10 @@ describe Rscons do
end end
it 'prints short representations of the commands being executed' do it 'prints short representations of the commands being executed' do
lines = test_dir('header') test_dir('header')
Rscons::Environment.new do |env|
env.Program('header', Dir['*.c'])
end
lines.should == [ lines.should == [
'CC header.o', 'CC header.o',
'LD header', 'LD header',
@@ -67,68 +92,230 @@ describe Rscons do
it 'builds a C program with one source file and one header file' do it 'builds a C program with one source file and one header file' do
test_dir('header') test_dir('header')
Rscons::Environment.new do |env|
env.Program('header', Dir['*.c'])
end
File.exists?('header.o').should be_true File.exists?('header.o').should be_true
`./header`.should == "The value is 2\n" `./header`.should == "The value is 2\n"
end end
it 'rebuilds a C module when a header it depends on changes' do it 'rebuilds a C module when a header it depends on changes' do
test_dir('header') test_dir('header')
env = Rscons::Environment.new do |env|
env.Program('header', Dir['*.c'])
end
`./header`.should == "The value is 2\n" `./header`.should == "The value is 2\n"
file_sub('header.h') {|line| line.sub(/2/, '5')} file_sub('header.h') {|line| line.sub(/2/, '5')}
build_testdir env.process
`./header`.should == "The value is 5\n" `./header`.should == "The value is 5\n"
end end
it 'does not rebuild a C module when its dependencies have not changed' do it 'does not rebuild a C module when its dependencies have not changed' do
lines = test_dir('header') test_dir('header')
env = Rscons::Environment.new do |env|
env.Program('header', Dir['*.c'])
end
`./header`.should == "The value is 2\n" `./header`.should == "The value is 2\n"
lines.should == [ lines.should == [
'CC header.o', 'CC header.o',
'LD header', 'LD header',
] ]
lines = build_testdir env.process
lines.should == [] lines.should == []
end end
it "does not rebuild a C module when only the file's timestampe has changed" do it "does not rebuild a C module when only the file's timestampe has changed" do
lines = test_dir('header') test_dir('header')
env = Rscons::Environment.new do |env|
env.Program('header', Dir['*.c'])
end
`./header`.should == "The value is 2\n" `./header`.should == "The value is 2\n"
lines.should == [ lines.should == [
'CC header.o', 'CC header.o',
'LD header', 'LD header',
] ]
file_sub('header.c') {|line| line} file_sub('header.c') {|line| line}
lines = build_testdir env.process
lines.should == [] lines.should == []
end end
it 're-links a program when the link flags have changed' do it 're-links a program when the link flags have changed' do
lines = test_dir('simple') test_dir('simple')
Rscons::Environment.new(echo: :command) do |env|
env.Program('simple', Dir['*.c'])
end
lines.should == [ lines.should == [
'gcc -c -o simple.o -MMD -MF simple.mf simple.c', 'gcc -c -o simple.o -MMD -MF simple.mf simple.c',
'gcc -o simple simple.o', 'gcc -o simple simple.o',
] ]
file_sub('build.rb') {|line| line.sub(/.*CHANGE.FLAGS.*/, ' env["LIBS"] += ["c"]')} Rscons::Environment.new(echo: :command) do |env|
lines = build_testdir env["LIBS"] += ["c"]
env.Program('simple', Dir['*.c'])
end
lines.should == ['gcc -o simple simple.o -lc'] lines.should == ['gcc -o simple simple.o -lc']
end end
it 'builds object files in a different build directory' do it 'builds object files in a different build directory' do
lines = test_dir('build_dir') test_dir('build_dir')
Rscons::Environment.new do |env|
env.append('CPPPATH' => Dir['src/**/*/'])
env.build_dir(%r{^src/([^/]+)/}, 'build_\\1/')
env.Program('build_dir', Dir['src/**/*.c'])
end
`./build_dir`.should == "Hello from two()\n" `./build_dir`.should == "Hello from two()\n"
File.exists?('build/one/one.o').should be_true File.exists?('build_one/one.o').should be_true
File.exists?('build/two/two.o').should be_true File.exists?('build_two/two.o').should be_true
end
it 'uses build directories before build root' do
test_dir('build_dir')
Rscons::Environment.new do |env|
env.append('CPPPATH' => Dir['src/**/*/'])
env.build_dir("src", "build")
env.build_root = "build_root"
env.Program('build_dir', Dir['src/**/*.c'])
end
lines.should == ["CC build/one/one.o", "CC build/two/two.o", "LD build_dir"]
end
it 'uses build_root if no build directories match' do
test_dir('build_dir')
Rscons::Environment.new do |env|
env.append('CPPPATH' => Dir['src/**/*/'])
env.build_dir("src2", "build")
env.build_root = "build_root"
env.Program('build_dir', Dir['src/**/*.c'])
end
lines.should == ["CC build_root/src/one/one.o", "CC build_root/src/two/two.o", "LD build_dir"]
end
it "expands target and source paths starting with ^/ to be relative to the build root" do
test_dir('build_dir')
Rscons::Environment.new(echo: :command) do |env|
env.append('CPPPATH' => Dir['src/**/*/'])
env.build_root = "build_root"
FileUtils.mkdir_p(env.build_root)
FileUtils.mv("src/one/one.c", "build_root")
env.Object("^/one.o", "^/one.c")
env.Program('build_dir', Dir['src/**/*.c'] + ["^/one.o"])
end
lines.should == [
%q{gcc -c -o build_root/one.o -MMD -MF build_root/one.mf -Isrc/one/ -Isrc/two/ build_root/one.c},
%q{gcc -c -o build_root/src/two/two.o -MMD -MF build_root/src/two/two.mf -Isrc/one/ -Isrc/two/ src/two/two.c},
%q{gcc -o build_dir build_root/src/two/two.o build_root/one.o},
]
end
it 'cleans built files' do
test_dir('build_dir')
Rscons::Environment.new do |env|
env.append('CPPPATH' => Dir['src/**/*/'])
env.build_dir(%r{^src/([^/]+)/}, 'build_\\1/')
env.Program('build_dir', Dir['src/**/*.c'])
end
`./build_dir`.should == "Hello from two()\n"
File.exists?('build_one/one.o').should be_true
File.exists?('build_two/two.o').should be_true
Rscons.clean
File.exists?('build_one/one.o').should be_false
File.exists?('build_two/two.o').should be_false
File.exists?('build_one').should be_false
File.exists?('build_two').should be_false
File.exists?('src/one/one.c').should be_true
end
it 'does not clean created directories if other non-rscons-generated files reside there' do
test_dir('build_dir')
Rscons::Environment.new do |env|
env.append('CPPPATH' => Dir['src/**/*/'])
env.build_dir(%r{^src/([^/]+)/}, 'build_\\1/')
env.Program('build_dir', Dir['src/**/*.c'])
end
`./build_dir`.should == "Hello from two()\n"
File.exists?('build_one/one.o').should be_true
File.exists?('build_two/two.o').should be_true
File.open('build_two/tmp', 'w') { |fh| fh.puts "dum" }
Rscons.clean
File.exists?('build_one/one.o').should be_false
File.exists?('build_two/two.o').should be_false
File.exists?('build_one').should be_false
File.exists?('build_two').should be_true
File.exists?('src/one/one.c').should be_true
end end
it 'allows Ruby classes as custom builders to be used to construct files' do it 'allows Ruby classes as custom builders to be used to construct files' do
lines = test_dir('custom_builder') test_dir('custom_builder')
class MySource < Rscons::Builder
def run(target, sources, cache, env, vars)
File.open(target, 'w') do |fh|
fh.puts <<EOF
#define THE_VALUE 5678
EOF
end
target
end
end
Rscons::Environment.new do |env|
env.add_builder(MySource.new)
env.MySource('inc.h', [])
env.Program('program', Dir['*.c'])
end
lines.should == ['CC program.o', 'LD program'] lines.should == ['CC program.o', 'LD program']
File.exists?('inc.h').should be_true File.exists?('inc.h').should be_true
`./program`.should == "The value is 5678\n" `./program`.should == "The value is 5678\n"
end end
it 'supports custom builders with multiple targets' do
test_dir('custom_builder')
class CHGen < Rscons::Builder
def run(target, sources, cache, env, vars)
c_fname = target
h_fname = target.sub(/\.c$/, ".h")
unless cache.up_to_date?([c_fname, h_fname], "", sources, env)
puts "CHGen #{c_fname}"
File.open(c_fname, "w") {|fh| fh.puts "int THE_VALUE = 42;"}
File.open(h_fname, "w") {|fh| fh.puts "extern int THE_VALUE;"}
cache.register_build([c_fname, h_fname], "", sources, env)
end
target
end
end
env = Rscons::Environment.new do |env|
env.add_builder(CHGen.new)
env.CHGen("inc.c", ["program.c"])
env.Program("program", Dir["*.c"] + ["inc.c"])
end
lines.should == ["CHGen inc.c", "CC program.o", "CC inc.o", "LD program"]
File.exists?("inc.c").should be_true
File.exists?("inc.h").should be_true
`./program`.should == "The value is 42\n"
File.open("inc.c", "w") {|fh| fh.puts "int THE_VALUE = 33;"}
env.process
lines.should == ["CHGen inc.c"]
`./program`.should == "The value is 42\n"
end
it 'allows cloning Environment objects' do it 'allows cloning Environment objects' do
lines = test_dir('clone_env') test_dir('clone_env')
debug = Rscons::Environment.new(echo: :command) do |env|
env.build_dir('src', 'debug')
env['CFLAGS'] = '-O2'
env['CPPFLAGS'] = '-DSTRING="Debug Version"'
env.Program('program-debug', Dir['src/*.c'])
end
release = debug.clone do |env|
env["CPPFLAGS"] = '-DSTRING="Release Version"'
env.build_dir('src', 'release')
env.Program('program-release', Dir['src/*.c'])
end
lines.should == [ lines.should == [
%q{gcc -c -o debug/program.o -MMD -MF debug/program.mf '-DSTRING="Debug Version"' -O2 src/program.c}, %q{gcc -c -o debug/program.o -MMD -MF debug/program.mf '-DSTRING="Debug Version"' -O2 src/program.c},
%q{gcc -o program-debug debug/program.o}, %q{gcc -o program-debug debug/program.o},
@@ -137,10 +324,204 @@ describe Rscons do
] ]
end end
it 'allows cloning all attributes of an Environment object' do
test_dir('clone_env')
env1 = Rscons::Environment.new(echo: :command) do |env|
env.build_dir('src', 'build')
env['CFLAGS'] = '-O2'
env.add_build_hook do |build_op|
build_op[:vars]['CPPFLAGS'] = '-DSTRING="Hello"'
end
env.Program('program', Dir['src/*.c'])
end
env2 = env1.clone(clone: :all) do |env|
env.Program('program2', Dir['src/*.c'])
end
lines.should == [
%q{gcc -c -o build/program.o -MMD -MF build/program.mf -DSTRING="Hello" -O2 src/program.c},
%q{gcc -o program build/program.o},
%q{gcc -o program2 build/program.o},
]
end
it 'builds a C++ program with one source file' do it 'builds a C++ program with one source file' do
test_dir('simple_cc') test_dir('simple_cc')
Rscons::Environment.new do |env|
env.Program('simple', Dir['*.cc'])
end
File.exists?('simple.o').should be_true File.exists?('simple.o').should be_true
`./simple`.should == "This is a simple C++ program\n" `./simple`.should == "This is a simple C++ program\n"
end end
it 'allows overriding construction variables for individual builder calls' do
test_dir('two_sources')
Rscons::Environment.new(echo: :command) do |env|
env.Object("one.o", "one.c", 'CPPFLAGS' => ['-DONE'])
env.Program('two_sources', ['one.o', 'two.c'])
end
lines.should == [
'gcc -c -o one.o -MMD -MF one.mf -DONE one.c',
'gcc -c -o two.o -MMD -MF two.mf two.c',
'gcc -o two_sources one.o two.o',
]
File.exists?('two_sources').should be_true
`./two_sources`.should == "This is a C program with two sources.\n"
end
it 'builds a static library archive' do
test_dir('library')
Rscons::Environment.new(echo: :command) do |env|
env.Program('library', ['lib.a', 'three.c'])
env.Library("lib.a", ['one.c', 'two.c'], 'CPPFLAGS' => ['-Dmake_lib'])
end
lines.should == [
'gcc -c -o one.o -MMD -MF one.mf -Dmake_lib one.c',
'gcc -c -o two.o -MMD -MF two.mf -Dmake_lib two.c',
'ar rcs lib.a one.o two.o',
'gcc -c -o three.o -MMD -MF three.mf three.c',
'gcc -o library lib.a three.o',
]
File.exists?('library').should be_true
`ar t lib.a`.should == "one.o\ntwo.o\n"
end
it 'supports build hooks to override construction variables' do
test_dir("build_dir")
Rscons::Environment.new(echo: :command) do |env|
env.append('CPPPATH' => Dir['src/**/*/'])
env.build_dir(%r{^src/([^/]+)/}, 'build_\\1/')
env.add_build_hook do |build_op|
if build_op[:target] =~ %r{build_one/.*\.o}
build_op[:vars]["CFLAGS"] << "-O1"
elsif build_op[:target] =~ %r{build_two/.*\.o}
build_op[:vars]["CFLAGS"] << "-O2"
end
end
env.Program('build_hook', Dir['src/**/*.c'])
end
`./build_hook`.should == "Hello from two()\n"
lines.should =~ [
'gcc -c -o build_one/one.o -MMD -MF build_one/one.mf -Isrc/one/ -Isrc/two/ -O1 src/one/one.c',
'gcc -c -o build_two/two.o -MMD -MF build_two/two.mf -Isrc/one/ -Isrc/two/ -O2 src/two/two.c',
'gcc -o build_hook build_one/one.o build_two/two.o',
]
end
it 'rebuilds when user-specified dependencies change' do
test_dir('simple')
Rscons::Environment.new do |env|
env.Program('simple', Dir['*.c'])
File.open("file.ld", "w") do |fh|
fh.puts("foo")
end
env.depends('simple', 'file.ld')
end
lines.should == ["CC simple.o", "LD simple"]
File.exists?('simple.o').should be_true
`./simple`.should == "This is a simple C program\n"
Rscons::Environment.new do |env|
env.Program('simple', Dir['*.c'])
File.open("file.ld", "w") do |fh|
fh.puts("bar")
end
env.depends('simple', 'file.ld')
end
lines.should == ["LD simple"]
Rscons::Environment.new do |env|
env.Program('simple', Dir['*.c'])
File.unlink("file.ld")
end
lines.should == ["LD simple"]
Rscons::Environment.new do |env|
env.Program('simple', Dir['*.c'])
end
lines.should == []
end
unless ENV["omit_gdc_tests"]
it "supports building D sources" do
test_dir("d")
Rscons::Environment.new(echo: :command) do |env|
env.Program("hello-d", Dir["*.d"])
end
lines.should == [
"gdc -c -o main.o main.d",
"gdc -o hello-d main.o",
]
`./hello-d`.rstrip.should == "Hello from D!"
end
end
it "supports disassembling object files" do
test_dir("simple")
Rscons::Environment.new do |env|
env.Object("simple.o", "simple.c")
env.Disassemble("simple.txt", "simple.o")
end
File.exists?("simple.txt").should be_true
File.read("simple.txt").should =~ /Disassembly of section .text:/
end
it "supports preprocessing C sources" do
test_dir("simple")
Rscons::Environment.new do |env|
env.Preprocess("simplepp.c", "simple.c")
env.Program("simple", "simplepp.c")
end
File.read("simplepp.c").should =~ /# \d+ "simple.c"/
`./simple`.should == "This is a simple C program\n"
end
it "supports preprocessing C++ sources" do
test_dir("simple_cc")
Rscons::Environment.new do |env|
env.Preprocess("simplepp.cc", "simple.cc")
env.Program("simple", "simplepp.cc")
end
File.read("simplepp.cc").should =~ /# \d+ "simple.cc"/
`./simple`.should == "This is a simple C++ program\n"
end
it "supports invoking builders with no sources and a build_root defined" do
class TestBuilder < Rscons::Builder
def run(target, sources, cache, env, vars)
target
end
end
test_dir("simple")
Rscons::Environment.new do |env|
env.build_root = "build"
env.add_builder(TestBuilder.new)
env.TestBuilder("file")
end
end
it "expands construction variables in builder target and sources before invoking the builder" do
test_dir('custom_builder')
class MySource < Rscons::Builder
def run(target, sources, cache, env, vars)
File.open(target, 'w') do |fh|
fh.puts <<EOF
#define THE_VALUE 678
EOF
end
target
end
end
Rscons::Environment.new do |env|
env["hdr"] = "inc.h"
env["src"] = "program.c"
env.add_builder(MySource.new)
env.MySource('${hdr}')
env.Program('program', "${src}")
end
lines.should == ['CC program.o', 'LD program']
File.exists?('inc.h').should be_true
`./program`.should == "The value is 678\n"
end
end end
+23
View File
@@ -0,0 +1,23 @@
module Rscons
module Builders
describe CFile do
let(:env) {Environment.new}
subject {CFile.new}
it "invokes bison to create a .c file from a .y file" do
subject.should_receive(:standard_build).with("YACC parser.c", "parser.c", ["bison", "-d", "-o", "parser.c", "parser.y"], ["parser.y"], env, :cache)
subject.run("parser.c", ["parser.y"], :cache, env, {})
end
it "invokes a custom lexer to create a .cc file from a .ll file" do
env["LEX"] = "custom_lex"
subject.should_receive(:standard_build).with("LEX lexer.cc", "lexer.cc", ["custom_lex", "-o", "lexer.cc", "parser.ll"], ["parser.ll"], env, :cache)
subject.run("lexer.cc", ["parser.ll"], :cache, env, {})
end
it "raises an error when an unknown source file is specified" do
expect {subject.run("file.c", ["foo.bar"], :cache, env, {})}.to raise_error /Unknown source file .foo.bar. for CFile builder/
end
end
end
end
+271
View File
@@ -0,0 +1,271 @@
module Rscons
describe Cache do
before do
File.stub(:read) { nil }
end
def build_from(cache)
JSON.stub(:load) do
cache
end
Cache.instance.tap do |cache|
cache.send(:initialize!)
end
end
describe "#initialize" do
context "when corrupt" do
it "prints a warning and defaults to an empty hash" do
JSON.should_receive(:load).and_return("string")
$stderr.should_receive(:puts).with(/Warning:.*was.corrupt/)
c = Cache.instance
c.send(:initialize!)
c.instance_variable_get(:@cache).is_a?(Hash).should be_true
end
end
end
describe "#clear" do
it "removes the cache file" do
FileUtils.should_receive(:rm_f).with(Cache::CACHE_FILE)
JSON.stub(:load) {{}}
Cache.instance.clear
end
end
describe "#write" do
it "should fill in 'version' and write to file" do
cache = {}
fh = $stdout
fh.should_receive(:puts)
File.should_receive(:open).and_yield(fh)
build_from(cache).write
cache["version"].should == Rscons::VERSION
end
end
describe "#up_to_date?" do
empty_env = "env"
before do
empty_env.stub(:get_user_deps) { nil }
end
it "returns false when target file does not exist" do
File.should_receive(:exists?).with("target").and_return(false)
build_from({}).up_to_date?("target", "command", [], empty_env).should be_false
end
it "returns false when target is not registered in the cache" do
File.should_receive(:exists?).with("target").and_return(true)
build_from({}).up_to_date?("target", "command", [], empty_env).should be_false
end
it "returns false when the target's checksum does not match" do
_cache = {"targets" => {"target" => {"checksum" => "abc"}}}
cache = build_from(_cache)
File.should_receive(:exists?).with("target").and_return(true)
cache.should_receive(:calculate_checksum).with("target").and_return("def")
cache.up_to_date?("target", "command", [], empty_env).should be_false
end
it "returns false when the build command has changed" do
_cache = {"targets" => {"target" => {"checksum" => "abc", "command" => Digest::MD5.hexdigest("old command".inspect)}}}
cache = build_from(_cache)
File.should_receive(:exists?).with("target").and_return(true)
cache.should_receive(:calculate_checksum).with("target").and_return("abc")
cache.up_to_date?("target", "command", [], empty_env).should be_false
end
it "returns false when there is a new dependency" do
_cache = {"targets" => {"target" => {"checksum" => "abc",
"command" => Digest::MD5.hexdigest("command".inspect),
"deps" => [{"fname" => "dep.1"}]}}}
cache = build_from(_cache)
File.should_receive(:exists?).with("target").and_return(true)
cache.should_receive(:calculate_checksum).with("target").and_return("abc")
cache.up_to_date?("target", "command", ["dep.1", "dep.2"], empty_env).should be_false
end
it "returns false when a dependency's checksum has changed" do
_cache = {"targets" => {"target" => {"checksum" => "abc",
"command" => Digest::MD5.hexdigest("command".inspect),
"deps" => [{"fname" => "dep.1",
"checksum" => "dep.1.chk"},
{"fname" => "dep.2",
"checksum" => "dep.2.chk"},
{"fname" => "extra.dep",
"checksum" => "extra.dep.chk"}],
"user_deps" => []}}}
cache = build_from(_cache)
File.should_receive(:exists?).with("target").and_return(true)
cache.should_receive(:calculate_checksum).with("target").and_return("abc")
cache.should_receive(:calculate_checksum).with("dep.1").and_return("dep.1.chk")
cache.should_receive(:calculate_checksum).with("dep.2").and_return("dep.2.changed")
cache.up_to_date?("target", "command", ["dep.1", "dep.2"], empty_env).should be_false
end
it "returns false with strict_deps=true when cache has an extra dependency" do
_cache = {"targets" => {"target" => {"checksum" => "abc",
"command" => Digest::MD5.hexdigest("command".inspect),
"deps" => [{"fname" => "dep.1",
"checksum" => "dep.1.chk"},
{"fname" => "dep.2",
"checksum" => "dep.2.chk"},
{"fname" => "extra.dep",
"checksum" => "extra.dep.chk"}],
"user_deps" => []}}}
cache = build_from(_cache)
File.should_receive(:exists?).with("target").and_return(true)
cache.should_receive(:calculate_checksum).with("target").and_return("abc")
cache.up_to_date?("target", "command", ["dep.1", "dep.2"], empty_env, strict_deps: true).should be_false
end
it "returns false when there is a new user dependency" do
_cache = {"targets" => {"target" => {"checksum" => "abc",
"command" => Digest::MD5.hexdigest("command".inspect),
"deps" => [{"fname" => "dep.1"}],
"user_deps" => []}}}
cache = build_from(_cache)
env = "env"
env.should_receive(:get_user_deps).with("target").and_return(["file.ld"])
File.should_receive(:exists?).with("target").and_return(true)
cache.should_receive(:calculate_checksum).with("target").and_return("abc")
cache.up_to_date?("target", "command", ["dep.1"], env).should be_false
end
it "returns false when a user dependency checksum has changed" do
_cache = {"targets" => {"target" => {"checksum" => "abc",
"command" => Digest::MD5.hexdigest("command".inspect),
"deps" => [{"fname" => "dep.1",
"checksum" => "dep.1.chk"},
{"fname" => "dep.2",
"checksum" => "dep.2.chk"},
{"fname" => "extra.dep",
"checksum" => "extra.dep.chk"}],
"user_deps" => [{"fname" => "user.dep",
"checksum" => "user.dep.chk"}]}}}
cache = build_from(_cache)
env = "env"
env.should_receive(:get_user_deps).with("target").and_return(["user.dep"])
File.should_receive(:exists?).with("target").and_return(true)
cache.should_receive(:calculate_checksum).with("target").and_return("abc")
cache.should_receive(:calculate_checksum).with("dep.1").and_return("dep.1.chk")
cache.should_receive(:calculate_checksum).with("dep.2").and_return("dep.2.chk")
cache.should_receive(:calculate_checksum).with("extra.dep").and_return("extra.dep.chk")
cache.should_receive(:calculate_checksum).with("user.dep").and_return("INCORRECT")
cache.up_to_date?("target", "command", ["dep.1", "dep.2"], env).should be_false
end
it "returns true when no condition for false is met" do
_cache = {"targets" => {"target" => {"checksum" => "abc",
"command" => Digest::MD5.hexdigest("command".inspect),
"deps" => [{"fname" => "dep.1",
"checksum" => "dep.1.chk"},
{"fname" => "dep.2",
"checksum" => "dep.2.chk"},
{"fname" => "extra.dep",
"checksum" => "extra.dep.chk"}],
"user_deps" => []}}}
cache = build_from(_cache)
File.should_receive(:exists?).with("target").and_return(true)
cache.should_receive(:calculate_checksum).with("target").and_return("abc")
cache.should_receive(:calculate_checksum).with("dep.1").and_return("dep.1.chk")
cache.should_receive(:calculate_checksum).with("dep.2").and_return("dep.2.chk")
cache.should_receive(:calculate_checksum).with("extra.dep").and_return("extra.dep.chk")
cache.up_to_date?("target", "command", ["dep.1", "dep.2"], empty_env).should be_true
end
end
describe "#register_build" do
it "stores the given information in the cache" do
_cache = {}
cache = build_from(_cache)
env = "env"
env.should_receive(:get_user_deps).with("the target").and_return(["user.dep"])
cache.should_receive(:calculate_checksum).with("the target").and_return("the checksum")
cache.should_receive(:calculate_checksum).with("dep 1").and_return("dep 1 checksum")
cache.should_receive(:calculate_checksum).with("dep 2").and_return("dep 2 checksum")
cache.should_receive(:calculate_checksum).with("user.dep").and_return("user.dep checksum")
cache.register_build("the target", "the command", ["dep 1", "dep 2"], env)
cached_target = cache.instance_variable_get(:@cache)["targets"]["the target"]
cached_target.should_not be_nil
cached_target["command"].should == Digest::MD5.hexdigest("the command".inspect)
cached_target["checksum"].should == "the checksum"
cached_target["deps"].should == [
{"fname" => "dep 1", "checksum" => "dep 1 checksum"},
{"fname" => "dep 2", "checksum" => "dep 2 checksum"},
]
cached_target["user_deps"].should == [
{"fname" => "user.dep", "checksum" => "user.dep checksum"},
]
end
end
describe "#targets" do
it "returns a list of targets that are cached" do
cache = {"targets" => {"t1" => {}, "t2" => {}, "t3" => {}}}
build_from(cache).targets.should == ["t1", "t2", "t3"]
end
end
describe "#mkdir_p" do
it "makes directories and records any created in the cache" do
_cache = {}
cache = build_from(_cache)
File.should_receive(:exists?).with("one").and_return(true)
File.should_receive(:exists?).with("one/two").and_return(false)
FileUtils.should_receive(:mkdir).with("one/two")
File.should_receive(:exists?).with("one/two/three").and_return(false)
FileUtils.should_receive(:mkdir).with("one/two/three")
File.should_receive(:exists?).with("one").and_return(true)
File.should_receive(:exists?).with("one/two").and_return(true)
File.should_receive(:exists?).with("one/two/four").and_return(false)
FileUtils.should_receive(:mkdir).with("one/two/four")
cache.mkdir_p("one/two/three")
cache.mkdir_p("one\\two\\four")
cache.directories.should == ["one/two", "one/two/three", "one/two/four"]
end
it "handles absolute paths" do
_cache = {}
cache = build_from(_cache)
File.should_receive(:exists?).with("/one").and_return(true)
File.should_receive(:exists?).with("/one/two").and_return(false)
FileUtils.should_receive(:mkdir).with("/one/two")
cache.mkdir_p("/one/two")
cache.directories.should == ["/one/two"]
end
end
describe "#directories" do
it "returns a list of directories that are cached" do
_cache = {"directories" => {"dir1" => true, "dir2" => true}}
build_from(_cache).directories.should == ["dir1", "dir2"]
end
end
describe "#lookup_checksum" do
it "does not re-calculate the checksum when it is already cached" do
cache = build_from({})
cache.instance_variable_set(:@lookup_checksums, {"f1" => "f1.chk"})
cache.should_not_receive(:calculate_checksum)
cache.send(:lookup_checksum, "f1").should == "f1.chk"
end
it "calls calculate_checksum when the checksum is not cached" do
cache = build_from({})
cache.should_receive(:calculate_checksum).with("f1").and_return("ck")
cache.send(:lookup_checksum, "f1").should == "ck"
end
end
describe "#calculate_checksum" do
it "calculates the MD5 of the file contents" do
contents = "contents"
File.should_receive(:read).with("fname", mode: "rb").and_return(contents)
Digest::MD5.should_receive(:hexdigest).with(contents).and_return("the_checksum")
build_from({}).send(:calculate_checksum, "fname").should == "the_checksum"
end
end
end
end
+360
View File
@@ -0,0 +1,360 @@
module Rscons
describe Environment do
describe "#initialize" do
it "adds the default builders when they are not excluded" do
env = Environment.new
env.builders.size.should be > 0
env.builders.map {|name, builder| builder.is_a?(Builder)}.all?.should be_true
env.builders.find {|name, builder| name == "Object"}.should_not be_nil
env.builders.find {|name, builder| name == "Program"}.should_not be_nil
env.builders.find {|name, builder| name == "Library"}.should_not be_nil
end
it "excludes the default builders with exclude_builders: :all" do
env = Environment.new(exclude_builders: true)
env.builders.size.should == 0
end
context "when a block is given" do
it "yields self and invokes #process()" do
env = Environment.new do |env|
env.should_receive(:process)
end
end
end
end
describe "#clone" do
it 'should create unique copies of each construction variable' do
env = Environment.new
env["CPPPATH"] << "path1"
env2 = env.clone
env2["CPPPATH"] << "path2"
env["CPPPATH"].should == ["path1"]
env2["CPPPATH"].should == ["path1", "path2"]
end
context "when a block is given" do
it "yields self and invokes #process()" do
env = Environment.new
env.clone do |env2|
env2.should_receive(:process)
end
end
end
end
describe "#add_builder" do
it "adds the builder to the list of builders" do
env = Environment.new(exclude_builders: true)
env.builders.keys.should == []
env.add_builder(Rscons::Builders::Object.new)
env.builders.keys.should == ["Object"]
end
end
describe "#get_build_fname" do
context "with no build directories" do
it "returns the name of the source file with suffix changed" do
env = Environment.new
env.get_build_fname("src/dir/file.c", ".o").should == "src/dir/file.o"
env.get_build_fname("src\\dir\\other.d", ".a").should == "src/dir/other.a"
env.get_build_fname("source.cc", ".o").should == "source.o"
end
context "with a build_root" do
it "uses the build_root unless the path is absolute" do
env = Environment.new
env.build_root = "build/proj"
env.get_build_fname("src/dir/file.c", ".o").should == "build/proj/src/dir/file.o"
env.get_build_fname("/some/lib.c", ".a").should == "/some/lib.a"
env.get_build_fname("C:\\abspath\\mod.cc", ".o").should == "C:/abspath/mod.o"
env.get_build_fname("build\\proj\\generated.c", ".o").should == "build/proj/generated.o"
env.get_build_fname("build/proj.XX", ".yy").should == "build/proj/build/proj.yy"
end
end
end
context "with build directories" do
it "uses the build directories to create the output file name" do
env = Environment.new
env.build_dir("src", "bld")
env.build_dir(%r{^libs/([^/]+)}, 'build/libs/\1')
env.get_build_fname("src/input.cc", ".o").should == "bld/input.o"
env.get_build_fname("libs/lib1/some/file.c", ".o").should == "build/libs/lib1/some/file.o"
env.get_build_fname("libs/otherlib/otherlib.cc", ".o").should == "build/libs/otherlib/otherlib.o"
env.get_build_fname("other_directory/o.d", ".a").should == "other_directory/o.a"
end
context "with a build_root" do
it "uses the build_root unless a build directory matches or the path is absolute" do
env = Environment.new
env.build_dir("src", "bld")
env.build_dir(%r{^libs/([^/]+)}, 'build/libs/\1')
env.build_root = "bldit"
env.get_build_fname("src/input.cc", ".o").should == "bld/input.o"
env.get_build_fname("libs/lib1/some/file.c", ".o").should == "build/libs/lib1/some/file.o"
env.get_build_fname("libs/otherlib/otherlib.cc", ".o").should == "build/libs/otherlib/otherlib.o"
env.get_build_fname("other_directory/o.d", ".a").should == "bldit/other_directory/o.a"
env.get_build_fname("bldit/some/mod.d", ".a").should == "bldit/some/mod.a"
end
end
end
end
describe "#[]" do
it "allows reading construction variables" do
env = Environment.new
env["CFLAGS"] = ["-g", "-Wall"]
env["CFLAGS"].should == ["-g", "-Wall"]
end
end
describe "#[]=" do
it "allows writing construction variables" do
env = Environment.new
env["CFLAGS"] = ["-g", "-Wall"]
env["CFLAGS"] -= ["-g"]
env["CFLAGS"] += ["-O3"]
env["CFLAGS"].should == ["-Wall", "-O3"]
env["other_var"] = "val33"
env["other_var"].should == "val33"
end
end
describe "#append" do
it "allows adding many construction variables at once" do
env = Environment.new
env["CFLAGS"] = ["-g"]
env["CPPPATH"] = ["inc"]
env.append("CFLAGS" => ["-Wall"], "CPPPATH" => ["include"])
env["CFLAGS"].should == ["-Wall"]
env["CPPPATH"].should == ["include"]
end
end
describe "#process" do
it "runs builders for all of the targets specified" do
env = Environment.new
env.Program("a.out", "main.c")
cache = "cache"
Cache.should_receive(:instance).and_return(cache)
cache.should_receive(:clear_checksum_cache!)
env.should_receive(:run_builder).with(anything, "a.out", ["main.c"], cache, {}).and_return(true)
cache.should_receive(:write)
env.process
end
it "builds dependent targets first" do
env = Environment.new
env.Program("a.out", "main.o")
env.Object("main.o", "other.cc")
cache = "cache"
Cache.should_receive(:instance).and_return(cache)
cache.should_receive(:clear_checksum_cache!)
env.should_receive(:run_builder).with(anything, "main.o", ["other.cc"], cache, {}).and_return("main.o")
env.should_receive(:run_builder).with(anything, "a.out", ["main.o"], cache, {}).and_return("a.out")
cache.should_receive(:write)
env.process
end
it "raises a BuildError when building fails" do
env = Environment.new
env.Program("a.out", "main.o")
env.Object("main.o", "other.cc")
cache = "cache"
Cache.should_receive(:instance).and_return(cache)
cache.should_receive(:clear_checksum_cache!)
env.should_receive(:run_builder).with(anything, "main.o", ["other.cc"], cache, {}).and_return(false)
cache.should_receive(:write)
expect { env.process }.to raise_error BuildError, /Failed.to.build.main.o/
end
it "writes the cache when the Builder raises an exception" do
env = Environment.new
env.Object("module.o", "module.c")
cache = "cache"
Cache.should_receive(:instance).and_return(cache)
cache.should_receive(:clear_checksum_cache!)
env.stub(:run_builder) do |builder, target, sources, cache, vars|
raise "Ruby exception thrown by builder"
end
cache.should_receive(:write)
expect { env.process }.to raise_error RuntimeError, /Ruby exception thrown by builder/
end
end
describe "#clear_targets" do
it "resets @targets to an empty hash" do
env = Environment.new
env.Program("a.out", "main.o")
expect(env.instance_variable_get(:@targets).keys).to eq(["a.out"])
env.clear_targets
expect(env.instance_variable_get(:@targets).keys).to eq([])
end
end
describe "#build_command" do
it "returns a command based on the variables in the Environment" do
env = Environment.new
env["path"] = ["dir1", "dir2"]
env["flags"] = ["-x", "-y", "${specialflag}"]
env["specialflag"] = "-z"
template = ["cmd", "-I${path}", "${flags}", "${_source}", "${_dest}"]
cmd = env.build_command(template, "_source" => "infile", "_dest" => "outfile")
cmd.should == ["cmd", "-Idir1", "-Idir2", "-x", "-y", "-z", "infile", "outfile"]
end
end
describe "#expand_varref" do
it "returns the fully expanded variable reference" do
env = Environment.new
env["path"] = ["dir1", "dir2"]
env["flags"] = ["-x", "-y", "${specialflag}"]
env["specialflag"] = "-z"
env.expand_varref(["-p${path}", "${flags}"]).should == ["-pdir1", "-pdir2", "-x", "-y", "-z"]
env.expand_varref("foo").should == "foo"
expect {env.expand_varref("${foo}")}.to raise_error /expand.a.variable.reference/
env.expand_varref("${specialflag}").should == "-z"
env.expand_varref("${path}").should == ["dir1", "dir2"]
end
end
describe "#execute" do
context "with echo: :short" do
context "with no errors" do
it "prints the short description and executes the command" do
env = Environment.new(echo: :short)
env.should_receive(:puts).with("short desc")
env.should_receive(:system).with("a", "command").and_return(true)
env.execute("short desc", ["a", "command"])
end
end
context "with errors" do
it "prints the short description, executes the command, and prints the failed command line" do
env = Environment.new(echo: :short)
env.should_receive(:puts).with("short desc")
env.should_receive(:system).with("a", "command").and_return(false)
$stdout.should_receive(:write).with("Failed command was: ")
env.should_receive(:puts).with("a command")
env.execute("short desc", ["a", "command"])
end
end
end
context "with echo: :command" do
it "prints the command executed and executes the command" do
env = Environment.new(echo: :command)
env.should_receive(:puts).with("a command '--arg=val with spaces'")
env.should_receive(:system).with({modified: :environment}, "a", "command", "--arg=val with spaces", {opt: :val}).and_return(false)
env.execute("short desc", ["a", "command", "--arg=val with spaces"], env: {modified: :environment}, options: {opt: :val})
end
end
end
describe "#method_missing" do
it "calls the original method missing when the target method is not a known builder" do
env = Environment.new
expect {env.foobar}.to raise_error /undefined method .foobar./
end
it "records the target when the target method is a known builder" do
env = Environment.new
env.instance_variable_get(:@targets).should == {}
env.Program("target", ["src1", "src2"], var: "val")
target = env.instance_variable_get(:@targets)["target"]
target.should_not be_nil
target[:builder].is_a?(Builder).should be_true
target[:sources].should == ["src1", "src2"]
target[:vars].should == {var: "val"}
target[:args].should == []
end
it "raises an error when vars is not a Hash" do
env = Environment.new
expect { env.Program("a.out", "main.c", "other") }.to raise_error /Unexpected construction variable set/
end
end
describe "#depends" do
it "records the given dependencies in @user_deps" do
env = Environment.new
env.depends("foo", "bar", "baz")
env.instance_variable_get(:@user_deps).should == {"foo" => ["bar", "baz"]}
end
it "records user dependencies only once" do
env = Environment.new
env.instance_variable_set(:@user_deps, {"foo" => ["bar"]})
env.depends("foo", "bar", "baz")
env.instance_variable_get(:@user_deps).should == {"foo" => ["bar", "baz"]}
end
end
describe "#build_sources" do
class ABuilder < Builder
def produces?(target, source, env)
target =~ /\.ab_out$/ and source =~ /\.ab_in$/
end
end
it "finds and invokes a builder to produce output files with the requested suffixes" do
cache = "cache"
env = Environment.new
env.add_builder(ABuilder.new)
env.builders["Object"].should_receive(:run).with("mod.o", ["mod.c"], cache, env, anything).and_return("mod.o")
env.builders["ABuilder"].should_receive(:run).with("mod2.ab_out", ["mod2.ab_in"], cache, env, anything).and_return("mod2.ab_out")
env.build_sources(["precompiled.o", "mod.c", "mod2.ab_in"], [".o", ".ab_out"], cache, {}).should == ["precompiled.o", "mod.o", "mod2.ab_out"]
end
end
describe "#run_builder" do
it "modifies the construction variables using given build hooks and invokes the builder" do
env = Environment.new
env.add_build_hook do |build_op|
if build_op[:sources].first =~ %r{src/special}
build_op[:vars]["CFLAGS"] += ["-O3", "-DSPECIAL"]
end
end
env.builders["Object"].stub(:run) do |target, sources, cache, env, vars|
vars["CFLAGS"].should == []
end
env.run_builder(env.builders["Object"], "build/normal/module.o", ["src/normal/module.c"], "cache", {})
env.builders["Object"].stub(:run) do |target, sources, cache, env, vars|
vars["CFLAGS"].should == ["-O3", "-DSPECIAL"]
end
env.run_builder(env.builders["Object"], "build/special/module.o", ["src/special/module.c"], "cache", {})
end
end
describe ".parse_makefile_deps" do
it 'handles dependencies on one line' do
File.should_receive(:read).with('makefile').and_return(<<EOS)
module.o: source.cc
EOS
Environment.parse_makefile_deps('makefile', 'module.o').should == ['source.cc']
end
it 'handles dependencies split across many lines' do
File.should_receive(:read).with('makefile').and_return(<<EOS)
module.o: module.c \\
module.h \\
other.h
EOS
Environment.parse_makefile_deps('makefile', 'module.o').should == [
'module.c', 'module.h', 'other.h']
end
end
end
end
+159
View File
@@ -0,0 +1,159 @@
module Rscons
describe VarSet do
describe '#initialize' do
it "initializes variables from a Hash" do
v = VarSet.new({"one" => 1, "two" => :two})
v["one"].should == 1
v["two"].should == :two
end
it "initializes variables from another VarSet" do
v = VarSet.new({"one" => 1})
v2 = VarSet.new(v)
v2["one"].should == 1
end
it "makes a deep copy of the given VarSet" do
v = VarSet.new({"array" => [1, 2, 3]})
v2 = VarSet.new(v)
v["array"] << 4
v["array"].should == [1, 2, 3, 4]
v2["array"].should == [1, 2, 3]
end
end
describe "#[]" do
it "allows accessing a variable with its verbatim value if type is not specified" do
v = VarSet.new({"fuz" => "a string", "foo" => 42, "bar" => :baz,
"qax" => [3, 6], "qux" => {a: :b}})
v["fuz"].should == "a string"
v["foo"].should == 42
v["bar"].should == :baz
v["qax"].should == [3, 6]
v["qux"].should == {a: :b}
end
end
describe "#[]=" do
it "allows assigning to variables" do
v = VarSet.new("CFLAGS" => ["-Wall", "-O3"])
v["CPPPATH"] = ["one", "two"]
v["CFLAGS"].should == ["-Wall", "-O3"]
v["CPPPATH"].should == ["one", "two"]
end
end
describe "#include?" do
it "returns whether the variable is in the VarSet" do
v = VarSet.new("CFLAGS" => [], :foo => :bar)
expect(v.include?("CFLAGS")).to be_true
expect(v.include?(:CFLAGS)).to be_false
expect(v.include?(:foo)).to be_true
expect(v.include?("foo")).to be_false
expect(v.include?("bar")).to be_false
v2 = v.clone
v2.append("bar" => [])
expect(v2.include?("CFLAGS")).to be_true
expect(v2.include?(:CFLAGS)).to be_false
expect(v2.include?(:foo)).to be_true
expect(v2.include?("foo")).to be_false
expect(v2.include?("bar")).to be_true
end
end
describe '#append' do
it "adds values from a Hash to the VarSet" do
v = VarSet.new("LDFLAGS" => "-lgcc")
v.append("LIBS" => "gcc", "LIBPATH" => ["mylibs"])
expect(v["LDFLAGS"]).to eq("-lgcc")
expect(v["LIBS"]).to eq("gcc")
expect(v["LIBPATH"]).to eq(["mylibs"])
end
it "adds values from another VarSet to the VarSet" do
v = VarSet.new("CPPPATH" => ["mydir"])
v2 = VarSet.new("CFLAGS" => ["-O0"], "CPPPATH" => ["different_dir"])
v.append(v2)
expect(v["CFLAGS"]).to eq(["-O0"])
expect(v["CPPPATH"]).to eq(["different_dir"])
end
it "does not pick up subsequent variable changes from a given VarSet" do
v = VarSet.new("dirs" => ["a"])
v2 = VarSet.new
v2.append(v)
v["dirs"] << "b"
expect(v["dirs"]).to eq(["a", "b"])
expect(v2["dirs"]).to eq(["a"])
end
end
describe '#merge' do
it "returns a new VarSet merged with the given Hash" do
v = VarSet.new("foo" => "yoda")
v2 = v.merge("baz" => "qux")
expect(v["foo"]).to eq("yoda")
expect(v2["foo"]).to eq("yoda")
expect(v2["baz"]).to eq("qux")
end
it "returns a new VarSet merged with the given VarSet" do
v = VarSet.new("foo" => ["a", "b"], "bar" => 42)
v2 = v.merge(VarSet.new("bar" => 33, "baz" => :baz))
v2["foo"] << "c"
expect(v["foo"]).to eq ["a", "b"]
expect(v["bar"]).to eq 42
expect(v2["foo"]).to eq ["a", "b", "c"]
expect(v2["bar"]).to eq 33
end
it "does not pick up subsequent variable changes from a given VarSet" do
v = VarSet.new("var" => ["a", "b"], "var2" => ["1", "2"])
v["var2"] << "3"
v2 = v.clone
v["var"] << "c"
expect(v["var"]).to eq(["a", "b", "c"])
expect(v["var2"]).to eq(["1", "2", "3"])
expect(v2["var"]).to eq(["a", "b"])
expect(v2["var2"]).to eq(["1", "2", "3"])
end
end
describe '#expand_varref' do
v = VarSet.new("CFLAGS" => ["-Wall", "-O2"],
"CC" => "gcc",
"CPPPATH" => ["dir1", "dir2"],
"compiler" => "${CC}",
"cmd" => ["${CC}", "-c", "${CFLAGS}", "-I${CPPPATH}"])
it "expands to the string itself if the string is not a variable reference" do
v.expand_varref("CC").should == "CC"
v.expand_varref("CPPPATH").should == "CPPPATH"
v.expand_varref("str").should == "str"
end
it "expands a single variable reference beginning with a '$'" do
v.expand_varref("${CC}").should == "gcc"
v.expand_varref("${CPPPATH}").should == ["dir1", "dir2"]
end
it "expands a single variable reference in ${arr} notation" do
v.expand_varref("prefix${CFLAGS}suffix").should == ["prefix-Wallsuffix", "prefix-O2suffix"]
v.expand_varref(v["cmd"]).should == ["gcc", "-c", "-Wall", "-O2", "-Idir1", "-Idir2"]
end
it "expands a variable reference recursively" do
v.expand_varref("${compiler}").should == "gcc"
v.expand_varref("${cmd}").should == ["gcc", "-c", "-Wall", "-O2", "-Idir1", "-Idir2"]
end
it "resolves multiple variable references in one element by enumerating all combinations" do
v.expand_varref("cflag: ${CFLAGS}, cpppath: ${CPPPATH}, compiler: ${compiler}").should == [
"cflag: -Wall, cpppath: dir1, compiler: gcc",
"cflag: -O2, cpppath: dir1, compiler: gcc",
"cflag: -Wall, cpppath: dir2, compiler: gcc",
"cflag: -O2, cpppath: dir2, compiler: gcc",
]
end
it "raises an error when a variable reference refers to a non-existent variable" do
expect { v.expand_varref("${not_here}") }.to raise_error /I do not know how to expand a variable reference to a NilClass/
end
end
end
end
+26
View File
@@ -0,0 +1,26 @@
describe Rscons do
describe ".clean" do
it "removes all build targets and created directories" do
cache = "cache"
Rscons::Cache.should_receive(:instance).and_return(cache)
cache.should_receive(:targets).and_return(["build/a.out", "build/main.o"])
FileUtils.should_receive(:rm_f).with("build/a.out")
FileUtils.should_receive(:rm_f).with("build/main.o")
cache.should_receive(:directories).and_return(["build/one", "build/one/two", "build", "other"])
File.should_receive(:directory?).with("build/one/two").and_return(true)
Dir.should_receive(:entries).with("build/one/two").and_return([".", ".."])
Dir.should_receive(:rmdir).with("build/one/two")
File.should_receive(:directory?).with("build/one").and_return(true)
Dir.should_receive(:entries).with("build/one").and_return([".", ".."])
Dir.should_receive(:rmdir).with("build/one")
File.should_receive(:directory?).with("build").and_return(true)
Dir.should_receive(:entries).with("build").and_return([".", ".."])
Dir.should_receive(:rmdir).with("build")
File.should_receive(:directory?).with("other").and_return(true)
Dir.should_receive(:entries).with("other").and_return([".", "..", "other.file"])
cache.should_receive(:clear)
Rscons.clean
end
end
end
+6
View File
@@ -1 +1,7 @@
require "simplecov"
SimpleCov.start do
add_filter "/spec/"
end
require "rscons" require "rscons"