From 6a8647a9332c6f0066e7fe00cd57d876411b454b Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Sun, 10 Sep 2023 10:00:41 -0400 Subject: [PATCH] Place object files next to source files for source files in build directory - close #171 --- build_tests/simple/build_root_source_path.rb | 13 +++++++++++++ lib/rscons/environment.rb | 10 +++++++++- spec/build_tests_spec.rb | 8 ++++++++ 3 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 build_tests/simple/build_root_source_path.rb diff --git a/build_tests/simple/build_root_source_path.rb b/build_tests/simple/build_root_source_path.rb new file mode 100644 index 0000000..051dbfa --- /dev/null +++ b/build_tests/simple/build_root_source_path.rb @@ -0,0 +1,13 @@ +env("e", echo: :command) do |env| + source_file = "#{env.build_root}/src/foo.c" + FileUtils.mkdir_p(File.dirname(source_file)) + File.open(source_file, "w") do |fh| + fh.puts(<<-EOF) + int main() + { + return 29; + } + EOF + end + env.Program("foo.exe", source_file) +end diff --git a/lib/rscons/environment.rb b/lib/rscons/environment.rb index 65cc73f..2a4a191 100644 --- a/lib/rscons/environment.rb +++ b/lib/rscons/environment.rb @@ -277,7 +277,15 @@ module Rscons if extra_path = builder_class.extra_path extra_path = "/#{extra_path}" end - "#{@build_root}/o#{extra_path}/#{Util.make_relative_path("#{source_fname}#{suffix}")}".gsub("\\", "/") + source_fname = source_fname.gsub("\\", "/") + if extra_path.nil? && source_fname.start_with?("#{@build_root}/") + # If the source file is in the environment's build directory and no + # builder "extra path" is present, then just place the output file + # next to the source file. + "#{source_fname}#{suffix}" + else + "#{@build_root}/o#{extra_path}/#{Util.make_relative_path("#{source_fname}#{suffix}")}" + end end # Build all build targets specified in the Environment. diff --git a/spec/build_tests_spec.rb b/spec/build_tests_spec.rb index e35fcad..c293504 100644 --- a/spec/build_tests_spec.rb +++ b/spec/build_tests_spec.rb @@ -990,6 +990,14 @@ EOF verify_lines(slines, [%r{\babs.exe\b}]) end + it "creates object files next to the source file for source files in the build root" do + test_dir "simple" + result = run_rscons(args: %w[-f build_root_source_path.rb]) + expect(result.stderr).to eq "" + expect(File.exist?("build/e/o/build/e/src/foo.c.o")).to be_falsey + expect(File.exist?("build/e/src/foo.c.o")).to be_truthy + end + it "creates shared libraries" do test_dir("shared_library")