diff --git a/build_tests/simple/path_append/foobar b/build_tests/simple/path_append/foobar new file mode 100755 index 0000000..aba5a87 --- /dev/null +++ b/build_tests/simple/path_append/foobar @@ -0,0 +1,3 @@ +#!/bin/sh + +echo "foobar!" diff --git a/build_tests/simple/path_append/gcc b/build_tests/simple/path_append/gcc new file mode 100755 index 0000000..0a31721 --- /dev/null +++ b/build_tests/simple/path_append/gcc @@ -0,0 +1,3 @@ +#!/bin/sh + +exit 42 diff --git a/build_tests/simple/path_prepend/flex b/build_tests/simple/path_prepend/flex new file mode 100755 index 0000000..10bb6f1 --- /dev/null +++ b/build_tests/simple/path_prepend/flex @@ -0,0 +1,3 @@ +#!/bin/sh + +echo "flex!" diff --git a/build_tests/simple/pathing.rb b/build_tests/simple/pathing.rb new file mode 100644 index 0000000..47a8039 --- /dev/null +++ b/build_tests/simple/pathing.rb @@ -0,0 +1,10 @@ +path_prepend "path_prepend" +path_append "path_append" + +build do + Environment.new do |env| + system("flex") + system("foobar") + env.Object("simple.o", "simple.c") + end +end diff --git a/doc/user_guide.md b/doc/user_guide.md index 6f53666..05ed0c0 100644 --- a/doc/user_guide.md +++ b/doc/user_guide.md @@ -932,6 +932,22 @@ subsidiary build script. Subsidiary build scripts are executed from within the directory containing the build script. +###> PATH Management + +`rscons` provides methods for management of the `PATH` environment variable. + +The `path_append` and `path_prepend` methods can be used to append or prepend +a path to the `PATH` environment variable. + +```ruby +path_prepend "i686-elf-gcc/bin" +``` + +The `path_set` method sets the `PATH` environment variable to the given +Array or String. +The `path_components` method returns an Array of the components in the `PATH` +environment variable. + ##> Extending Rscons ### Adding New Languages diff --git a/lib/rscons/script.rb b/lib/rscons/script.rb index 4f2ece2..6077b75 100644 --- a/lib/rscons/script.rb +++ b/lib/rscons/script.rb @@ -6,6 +6,47 @@ module Rscons # Global DSL methods. class GlobalDsl + # Return path components from the PATH variable. + # + # @return [Array] + # Path components from the PATH variable. + def path_components + ENV["PATH"].split(File::PATH_SEPARATOR) + end + + # Prepend a path component to the PATH variable. + # + # @param path [String] + # Path to prepend. + # + # @return [void] + def path_prepend(path) + path_set([File.expand_path(path)] + path_components) + end + + # Append a path component to the PATH variable. + # + # @param path [String] + # Path to append. + # + # @return [void] + def path_append(path) + path_set(path_components + [File.expand_path(path)]) + end + + # Set the PATH variable. + # + # @param new_path [String, Array] + # New PATH variable value as an array or string. + # + # @return [void] + def path_set(new_path) + if new_path.is_a?(Array) + new_path = new_path.join(File::PATH_SEPARATOR) + end + ENV["PATH"] = new_path + end + # Invoke rscons in a subprocess for a subsidiary Rsconscript file. # # @param path [String] diff --git a/spec/build_tests_spec.rb b/spec/build_tests_spec.rb index 696fd12..b523256 100644 --- a/spec/build_tests_spec.rb +++ b/spec/build_tests_spec.rb @@ -1475,6 +1475,15 @@ EOF expect(result.status).to eq 0 end + it "allows prepending and appending to PATH" do + test_dir "simple" + result = run_rscons(rsconscript: "pathing.rb") + expect(result.stderr).to eq "" + expect(result.stdout).to match /flex!/ + expect(result.stdout).to match /foobar!/ + expect(File.exist?("simple.o")).to be_truthy + end + context "debugging" do it "prints a message when the target does not exist" do test_dir("simple")