Skip to content

Commit cb2c31f

Browse files
authored
Merge pull request swiftlang#410 from dmbryson/reldeps
[BuildSystem] Handle relative paths in 'shell' makefile-style dependencies
2 parents 7011c39 + 44cbe44 commit cb2c31f

File tree

2 files changed

+80
-2
lines changed

2 files changed

+80
-2
lines changed

lib/BuildSystem/BuildSystem.cpp

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
#ifdef _WIN32
5353
#include <Shlwapi.h>
5454
#else
55+
#include <limits.h>
5556
#include <fnmatch.h>
5657
#include <unistd.h>
5758
#endif
@@ -2074,7 +2075,21 @@ class ShellCommand : public ExternalCommand {
20742075
virtual void actOnRuleDependency(const char* dependency,
20752076
uint64_t length,
20762077
const StringRef unescapedWord) override {
2077-
bsci.taskDiscoveredDependency(task, BuildKey::makeNode(unescapedWord));
2078+
if (llvm::sys::path::is_absolute(unescapedWord)) {
2079+
bsci.taskDiscoveredDependency(task, BuildKey::makeNode(unescapedWord));
2080+
return;
2081+
}
2082+
2083+
// Generate absolute path
2084+
//
2085+
// NOTE: This is making the assumption that relative paths coming in a
2086+
// dependency file are in relation to the explictly set working
2087+
// directory, or the current working directory when it has not been set.
2088+
SmallString<PATH_MAX> absPath = StringRef(command->workingDirectory);
2089+
llvm::sys::path::append(absPath, unescapedWord);
2090+
llvm::sys::fs::make_absolute(absPath);
2091+
2092+
bsci.taskDiscoveredDependency(task, BuildKey::makeNode(absPath));
20782093
}
20792094

20802095
virtual void actOnRuleStart(const char* name, uint64_t length,
@@ -2191,7 +2206,12 @@ class ShellCommand : public ExternalCommand {
21912206
inheritEnv = value == "true";
21922207
} else if (name == "working-directory") {
21932208
#if __APPLE__
2194-
workingDirectory = value;
2209+
// Ensure the working directory is absolute. This will make sure any
2210+
// relative directories are interpreted as relative to the CWD at the time
2211+
// the rule is defined.
2212+
SmallString<PATH_MAX> wd = value;
2213+
llvm::sys::fs::make_absolute(wd);
2214+
workingDirectory = StringRef(wd);
21952215
#else
21962216
ctx.error("working-directory unsupported on this platform");
21972217
return false;
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# Check the handling of compiler-based discovered dependencies with relative
2+
# paths
3+
4+
# We test with a simple command that just writes a fake .d file which lists an
5+
# input dependency on "header-1".
6+
7+
# RUN: rm -rf %t.build
8+
# RUN: mkdir -p %t.build/wd
9+
# RUN: touch %t.build/wd/header-1 %t.build/wd/input-1
10+
# RUN: cp %s %t.build/build.llbuild
11+
12+
13+
# Check the first build.
14+
#
15+
# RUN: %{llbuild} buildsystem build --serial --chdir %t.build > %t1.out
16+
# RUN: %{FileCheck} --check-prefix=CHECK-INITIAL --input-file=%t1.out %s
17+
#
18+
# CHECK-INITIAL: CC output-1
19+
# CHECK-INITIAL: cat output-1 > ../output
20+
21+
22+
# Check a build that modifies the header.
23+
#
24+
# RUN: echo "mod" >> %t.build/header-1
25+
# RUN: %{llbuild} buildsystem build --serial --chdir %t.build &> %t2.out
26+
# RUN: %{FileCheck} --check-prefix=CHECK-AFTER-MOD --input-file=%t2.out %s
27+
#
28+
# CHECK-AFTER-MOD: CC output-1
29+
# CHECK-AFTER-MOD: cat output-1 > ../output
30+
#
31+
# REQUIRES: platform=Darwin
32+
33+
34+
client:
35+
name: basic
36+
37+
targets:
38+
"": ["output"]
39+
40+
commands:
41+
output-1:
42+
tool: shell
43+
inputs: ["wd/input-1"]
44+
outputs: ["wd/output-1"]
45+
args: "echo \"output-1: input-1 header-1\" > output-1.d && cat input-1 header-1 > output-1"
46+
description: CC output-1
47+
deps: output-1.d
48+
deps-style: makefile
49+
working-directory: wd
50+
51+
output:
52+
tool: shell
53+
inputs: ["wd/output-1"]
54+
outputs: ["output"]
55+
args: cat output-1 > ../output
56+
working-directory: wd
57+
58+

0 commit comments

Comments
 (0)