Skip to content

Commit d6f542c

Browse files
committed
Use basedir instead of os.getcwd().
1 parent 28ac93d commit d6f542c

File tree

9 files changed

+57
-54
lines changed

9 files changed

+57
-54
lines changed

modules/gmake/tests/cpp/test_make_pch.lua

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
local wks, prj
1919
function suite.setup()
20-
os.chdir(_TESTS_DIR)
2120
wks, prj = test.createWorkspace()
2221
end
2322

@@ -71,15 +70,32 @@
7170

7271

7372
--
74-
-- The PCH can be specified relative the an includes search path.
73+
-- The PCH can be specified relative to the includes search path.
7574
--
7675

77-
function suite.pch_searchesIncludeDirs()
76+
function suite.pch_searchesIncludeDirs_location()
7877
pchheader "premake.h"
79-
includedirs { "../../../src/host" }
78+
includedirs { "src/host" }
8079
prepareVars()
8180
test.capture [[
82-
PCH = ../../../src/host/premake.h
81+
PCH = src/host/premake.h
82+
]]
83+
end
84+
85+
86+
--
87+
-- The PCH can be specified relative to the includes search path.
88+
-- Due to the location being different from _MAIN_SCRIPT_DIR,
89+
-- the specified PCH path should be relative to the location.
90+
91+
92+
function suite.findsPCH_onIncludeDirs()
93+
location "MyProject"
94+
pchheader "premake.h"
95+
includedirs { "src/host" }
96+
prepareVars()
97+
test.capture [[
98+
PCH = ../src/host/premake.h
8399
]]
84100
end
85101

@@ -123,20 +139,3 @@ $(OBJECTS): | $(OBJDIR)
123139
endif
124140
]]
125141
end
126-
127-
128-
129-
--
130-
-- If the header is located on one of the include file
131-
-- search directories, it should get found automatically.
132-
--
133-
134-
function suite.findsPCH_onIncludeDirs()
135-
location "MyProject"
136-
pchheader "premake.h"
137-
includedirs { "../../../src/host" }
138-
prepareVars()
139-
test.capture [[
140-
PCH = ../../../../src/host/premake.h
141-
]]
142-
end

modules/gmake2/tests/test_gmake2_pch.lua

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020

2121
local wks, prj
2222
function suite.setup()
23-
os.chdir(_TESTS_DIR)
2423
gmake2.cpp.initialize()
2524
wks, prj = test.createWorkspace()
2625
end
@@ -95,15 +94,30 @@ GCH = $(PCH_PLACEHOLDER).gch
9594

9695

9796
--
98-
-- The PCH can be specified relative the an includes search path.
97+
-- The PCH can be specified relative to the includes search path.
9998
--
10099

101100
function suite.pch_searchesIncludeDirs()
102101
pchheader "premake.h"
103-
includedirs { "../../../src/host" }
102+
includedirs { "src/host" }
104103
prepareVars()
105104
test.capture [[
106-
PCH = ../../../src/host/premake.h
105+
PCH = src/host/premake.h
106+
]]
107+
end
108+
109+
--
110+
-- The PCH can be specified relative to the includes search path.
111+
-- Due to the location being different from _MAIN_SCRIPT_DIR,
112+
-- the specified PCH path should be relative to the location.
113+
114+
function suite.pch_searchesIncludeDirs_location()
115+
location "MyProject"
116+
pchheader "premake.h"
117+
includedirs { "src/host" }
118+
prepareVars()
119+
test.capture [[
120+
PCH = ../src/host/premake.h
107121
]]
108122
end
109123

@@ -160,23 +174,6 @@ endif
160174
]]
161175
end
162176

163-
164-
165-
--
166-
-- If the header is located on one of the include file
167-
-- search directories, it should get found automatically.
168-
--
169-
170-
function suite.findsPCH_onIncludeDirs()
171-
location "MyProject"
172-
pchheader "premake.h"
173-
includedirs { "../../../src/host" }
174-
prepareVars()
175-
test.capture [[
176-
PCH = ../../../../src/host/premake.h
177-
]]
178-
end
179-
180177
--
181178
-- If the header is located on one of the include file
182179
-- search directories, it should get found automatically.

modules/self-test/test_runner.lua

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,10 @@
9191
_TESTS_DIR = test.suite._TESTS_DIR
9292
_SCRIPT_DIR = test.suite._SCRIPT_DIR
9393

94+
-- Currently the test suite paths are tested against as if all tests are
95+
-- running with the repository root as their base working directory.
96+
_BASE_DIR = _MAIN_SCRIPT_DIR
97+
9498
m.suiteName = test.suiteName
9599
m.testName = test.testName
96100

src/base/api.lua

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@
148148
-- if I have an existing instance, create a new configuration
149149
-- block for it so I don't pick up an old filter
150150
if instance then
151-
configset.addFilter(instance, {}, os.getcwd())
151+
configset.addFilter(instance, {}, api.scope.current.basedir)
152152
end
153153

154154
-- otherwise, a new instance
@@ -163,7 +163,7 @@
163163
-- (recursive call, so needs to be its own function)
164164
api._clearContainerChildren(class)
165165

166-
-- active this container, as well as it ancestors
166+
-- active this container, as well as its ancestors
167167
if not class.placeholder then
168168
api.scope.current = instance
169169
end
@@ -716,7 +716,7 @@
716716
table.remove(api.scope.global.blocks, i)
717717
end
718718

719-
configset.addFilter(api.scope.current, {}, os.getcwd())
719+
configset.addFilter(api.scope.current, {}, api.scope.current.basedir)
720720
end
721721

722722

@@ -1087,7 +1087,7 @@
10871087
premake.field.kind("path", {
10881088
paths = true,
10891089
store = function(field, current, value, processor)
1090-
return path.deferredjoin(os.getcwd(), value)
1090+
return path.deferredjoin(p.api.scope.current.basedir, value)
10911091
end,
10921092
compare = function(field, a, b, processor)
10931093
return (a == b)
@@ -1152,7 +1152,7 @@
11521152
if (type(terms) == "table" and #terms == 1 and terms[1] == "*") or (terms == "*") then
11531153
terms = nil
11541154
end
1155-
local ok, err = configset.addFilter(api.scope.current, {terms}, os.getcwd())
1155+
local ok, err = configset.addFilter(api.scope.current, {terms}, api.scope.current.basedir)
11561156
if not ok then
11571157
error(err, 2)
11581158
end

src/base/container.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@
7878
self.name = name
7979
self.filename = name
8080
self.script = _SCRIPT
81-
self.basedir = os.getcwd()
81+
self.scriptdir = _SCRIPT_DIR
82+
self.basedir = _BASE_DIR or _SCRIPT_DIR
8283
self.external = false
8384

8485
for childClass in container.eachChildClass(class) do

src/base/context.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
-- expected that the creator of the context will set this value using
5050
-- the setbasedir() function.
5151

52-
ctx._basedir = os.getcwd()
52+
ctx._basedir = cfgset.basedir
5353

5454
-- when a missing field is requested, fetch it from my config
5555
-- set, and then cache the value for future lookups

tests/api/test_path_kind.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,5 @@
3434

3535
function suite.convertsToAbsolute()
3636
testapi "self/local.h"
37-
test.isequal(os.getcwd() .. "/self/local.h", api.scope.project.testapi)
37+
test.isequal(_BASE_DIR .. "/self/local.h", api.scope.project.testapi)
3838
end

tests/config/test_linkinfo.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
function suite.directoryIsTargetDir_onTargetDir()
3636
targetdir "../bin"
3737
i = prepare()
38-
test.isequal("../bin", path.getrelative(os.getcwd(), i.directory))
38+
test.isequal("../bin", path.getrelative(_BASE_DIR, i.directory))
3939
end
4040

4141

@@ -48,7 +48,7 @@
4848
targetdir "../bin"
4949
implibdir "../lib"
5050
i = prepare()
51-
test.isequal("../lib", path.getrelative(os.getcwd(), i.directory))
51+
test.isequal("../lib", path.getrelative(_BASE_DIR, i.directory))
5252
end
5353

5454

website/docs/basedir.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
Sets the base directory for a configuration, from with other paths contained by the configuration will be made relative at export time.
22

3+
This base directory is used when expanding path tokens encountered in non-path values. Such values will be made relative to this value so the resulting projects will only contain relative paths.
4+
35
```lua
46
basedir ("value")
57
```
68

7-
You do not normally need to set this value, as it is filled in automatically with the current working directory at the time the configuration block is created by the script.
9+
You do not normally need to set this value, as it is filled in automatically with the current script directory ([`_SCRIPT_DIR`](globals/premake_MAIN_SCRIPT_DIR.md)) at the time the configuration block is created by the script.
810

911
### Parameters ###
1012

0 commit comments

Comments
 (0)