From aafe2d6065864a5e1a39b32e389240738bc7a4e3 Mon Sep 17 00:00:00 2001 From: Vladimir Prus Date: Tue, 23 May 2006 14:21:58 +0000 Subject: [PATCH] Try to preserve relative paths in sources. Based on a patch from Ryan Gallagher. [SVN r34062] --- src/build/generators.jam | 17 +++++++++++++++-- src/build/project.jam | 12 +++++++++++- src/util/path.jam | 23 ++++++++++++++++++++--- test/relative_sources.py | 24 ++++++++++++++++++++++-- 4 files changed, 68 insertions(+), 8 deletions(-) diff --git a/src/build/generators.jam b/src/build/generators.jam index 66588a715..c19651f8b 100644 --- a/src/build/generators.jam +++ b/src/build/generators.jam @@ -105,6 +105,7 @@ class generator import virtual-target ; import "class" : new ; import property ; + import path ; EXPORT class@generator : indent increase-indent decrease-indent generators.dout ; @@ -414,8 +415,20 @@ class generator } } - # Names of sources might include directory. We should strip it. - name = $(name:D=) ; + # See if we need to add directory to the target name. + local dir = [ $(sources[1]).name ] ; + dir = $(dir:D) ; + if $(dir) && + # Never append '..' to target path. + ! [ MATCH .*(\\.\\.).* : $(dir) ] + && + ! [ path.is-rooted $(dir) ] + { + # Relative path is always relative to the source + # directory. Retain it, so that users can have files + # with the same in two different subdirectories. + name = $(dir)/$(name) ; + } } # Assign an action for each target diff --git a/src/build/project.jam b/src/build/project.jam index 32254750f..0e644690c 100644 --- a/src/build/project.jam +++ b/src/build/project.jam @@ -924,7 +924,17 @@ module project-rules # prject. So, just make the name absolute. for local p in $(paths) { - result += [ path.root $(p) [ path.pwd ] ] ; + # If the path is below source location, use relative path. + # Otherwise, use full path just to avoid any ambiguities. + local rel = [ path.relative $(p) $(location) : no-error ] ; + if $(rel) = not-a-child + { + result += [ path.root $(p) [ path.pwd ] ] ; + } + else + { + result += $(rel) ; + } } } else diff --git a/src/util/path.jam b/src/util/path.jam index cedff58a8..2134c1235 100644 --- a/src/util/path.jam +++ b/src/util/path.jam @@ -306,8 +306,9 @@ rule glob-in-parents ( dir : patterns + : upper-limit ? ) # Assuming 'child' is a subdirectory of 'parent', return the relative # path from 'parent' to 'child' # -rule relative ( child parent ) +rule relative ( child parent : no-error ? ) { + local not-a-child ; if $(parent) = "." { return $(child) ; @@ -326,10 +327,26 @@ rule relative ( child parent ) } else { - errors.error $(child) is not a subdir of $(parent) ; + not-a-child = true ; + split1 = ; } } - return [ join $(split2) ] ; + + if $(not-a-child) + { + if $(no-error) + { + return not-a-child ; + } + else + { + errors.error $(child) is not a subdir of $(parent) ; + } + } + else + { + return [ join $(split2) ] ; + } } } diff --git a/test/relative_sources.py b/test/relative_sources.py index 8bab211ad..b94e241a6 100644 --- a/test/relative_sources.py +++ b/test/relative_sources.py @@ -5,10 +5,30 @@ from BoostBuild import Tester t = Tester() -t.write("project-root.jam", "import gcc ;") -t.write("Jamfile", "exe a : src/a.cpp ;") +# Test that relative path to source, 'src', is preserved. +t.write("Jamroot", "exe a : src/a.cpp ;") t.write("src/a.cpp", "int main() { return 0; }\n") t.run_build_system() +t.expect_addition("bin/$toolset/debug/src/a.obj") + + +# Test that the relative path to source is preserved +# when using 'glob'. +t.rm("bin") +t.write("Jamroot", "exe a : [ glob src/*.cpp ] ;") +t.run_build_system() +t.expect_addition("bin/$toolset/debug/src/a.obj") + + +# Test that relative path with ".." is *not* added to +# target path. +t.rm(".") +t.write("Jamroot", "") +t.write("a.cpp", "int main() { return 0; }\n") +t.write("build/Jamfile", "exe a : ../a.cpp ; ") +t.run_build_system(subdir="build") +t.expect_addition("build/bin/$toolset/debug/a.obj") + t.cleanup()