Add Size builder - close #143

This commit is contained in:
Josh Holtrop 2022-01-17 12:50:38 -05:00
parent a316c4f922
commit 610b8f1266
6 changed files with 65 additions and 0 deletions

View File

@ -0,0 +1,6 @@
build do
Environment.new do |env|
env.Program("simple.exe", glob("*.c"))
env.Size("simple.size", "simple.exe")
end
end

View File

@ -625,6 +625,7 @@ There are several default builders that are built-in to Rscons:
library.
* `SharedObject`, which compiles source files to produce an object file, in a
way that is able to be used to create a shared library.
* `Size`, which runs the 'size' utility on an executable file.
####> The Command Builder
@ -840,6 +841,20 @@ allows it to be used to create a shared library are added.
Although it can be called explicitly, it is more commonly implicitly called by
the `SharedLibrary` builder.
####> The Size Builder
```ruby
env.Size(target, sources)
# Example
env.Program("program.exe", glob("*.c"))
env.Size("program.size", "program.exe")
```
The `Size` builder runs the "size" executable on the given source file and
stores its output in the target file.
The size executable can be specified with the `SIZE` construction variable,
and flags can be specified with `SIZEFLAGS`.
###> Phony Targets
rscons supports phony build targets.

View File

@ -33,6 +33,7 @@ module Rscons
:Program,
:SharedLibrary,
:SharedObject,
:Size,
]
# Class to represent a fatal error during an Rscons operation.
@ -146,6 +147,7 @@ require_relative "rscons/builders/program"
require_relative "rscons/builders/shared_library"
require_relative "rscons/builders/shared_object"
require_relative "rscons/builders/simple_builder"
require_relative "rscons/builders/size"
# language support
require_relative "rscons/builders/lang/asm"

View File

@ -0,0 +1,24 @@
module Rscons
module Builders
# Run the "size" utility on an executable and store its results in the
# target file.
# input file.
#
# Examples::
# env.Size("^/project.size", "^/project.elf")
class Size < Builder
# Run the builder to produce a build target.
def run(options)
if @command
finalize_command
else
@vars["_SOURCES"] = @sources
command = @env.build_command("${SIZECMD}", @vars)
standard_command("Size <source>#{Util.short_format_paths(@sources)}<reset> => <target>#{@target}<reset>", command, stdout: @target)
end
end
end
end
end

View File

@ -80,6 +80,9 @@ module Rscons
"SHLIBLINKPREFIX" => "-l",
"SHLIBPREFIX" => on_windows ? "" : "lib",
"SHLIBSUFFIX" => on_windows ? ".dll" : ".so",
"SIZE" => "size",
"SIZECMD" => %w[${SIZE} ${SIZEFLAGS} ${_SOURCES}],
"SIZEFLAGS" => [],
"YACC" => "bison",
"YACCSUFFIX" => %w[.y .yy],
"YACC_CMD" => %w[${YACC} ${YACC_FLAGS} -o ${_TARGET} ${_SOURCES}],

View File

@ -1672,6 +1672,21 @@ EOF
end
end
context "Size builder" do
it "generates a size file" do
test_dir "simple"
result = run_rscons(rsconscript: "size.rb")
verify_lines(lines(result.stdout), [
/Linking .*simple\.exe/,
/Size .*simple\.exe .*simple\.size/,
])
expect(File.exist?("simple.exe")).to be_truthy
expect(File.exist?("simple.size")).to be_truthy
expect(File.read("simple.size")).to match /text.*data.*bss/
end
end
context "multi-threading" do
it "waits for subcommands in threads for builders that support threaded commands" do
test_dir("simple")