Skip to content

Commit e8c7d2a

Browse files
committed
8332872: SetupExecute should cd to temp directory
Reviewed-by: erikj
1 parent dbf4fff commit e8c7d2a

File tree

5 files changed

+101
-15
lines changed

5 files changed

+101
-15
lines changed

make/CreateJmods.gmk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,7 @@ $(eval $(call SetupExecute, create_$(JMOD_FILE), \
257257
WARN := Creating $(INTERIM_MSG)$(JMOD_FILE), \
258258
DEPS := $(DEPS), \
259259
OUTPUT_FILE := $(JMODS_DIR)/$(JMOD_FILE), \
260+
WORKING_DIR := $(WORKSPACE_ROOT), \
260261
SUPPORT_DIR := $(JMODS_SUPPORT_DIR), \
261262
PRE_COMMAND := $(RM) $(JMODS_DIR)/$(JMOD_FILE) $(JMODS_SUPPORT_DIR)/$(JMOD_FILE), \
262263
COMMAND := $(JMOD) $(JMOD_SMALL_FLAGS) create --module-version $(VERSION_SHORT) \

make/UpdateSleefSource.gmk

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,8 @@ SLEEF_CMAKE_FILE := toolchains/$(OPENJDK_TARGET_CPU)-$(SLEEF_TOOLCHAIN_TYPE).cma
8181
$(eval $(call SetupExecute, sleef_native_config, \
8282
INFO := Configuring native sleef build, \
8383
OUTPUT_DIR := $(SLEEF_NATIVE_BUILD_DIR), \
84-
COMMAND := cd $(SLEEF_SOURCE_DIR) && $(CMAKE) -S . -B \
85-
$(SLEEF_NATIVE_BUILD_DIR), \
84+
WORKING_DIR := $(SLEEF_SOURCE_DIR), \
85+
COMMAND := $(CMAKE) -S . -B $(SLEEF_NATIVE_BUILD_DIR), \
8686
))
8787

8888
TARGETS := $(sleef_native_config)
@@ -91,8 +91,8 @@ $(eval $(call SetupExecute, sleef_native_build, \
9191
INFO := Building native sleef, \
9292
DEPS := $(sleef_native_config), \
9393
OUTPUT_DIR := $(SLEEF_NATIVE_BUILD_DIR), \
94-
COMMAND := cd $(SLEEF_SOURCE_DIR) && $(CMAKE) --build \
95-
$(SLEEF_NATIVE_BUILD_DIR) -j, \
94+
WORKING_DIR := $(SLEEF_SOURCE_DIR), \
95+
COMMAND := $(CMAKE) --build $(SLEEF_NATIVE_BUILD_DIR) -j, \
9696
))
9797

9898
TARGETS := $(sleef_native_build)
@@ -101,8 +101,8 @@ $(eval $(call SetupExecute, sleef_cross_config, \
101101
INFO := Configuring cross-compiling sleef build, \
102102
DEPS := $(sleef_native_build), \
103103
OUTPUT_DIR := $(SLEEF_CROSS_BUILD_DIR), \
104-
COMMAND := cd $(SLEEF_SOURCE_DIR) && $(CMAKE) -S . -B \
105-
$(SLEEF_CROSS_BUILD_DIR) \
104+
WORKING_DIR := $(SLEEF_SOURCE_DIR), \
105+
COMMAND := $(CMAKE) -S . -B $(SLEEF_CROSS_BUILD_DIR) \
106106
-DCMAKE_C_COMPILER=$(CC) \
107107
-DCMAKE_TOOLCHAIN_FILE=$(SLEEF_CMAKE_FILE) \
108108
-DNATIVE_BUILD_DIR=$(SLEEF_NATIVE_BUILD_DIR) \
@@ -116,8 +116,8 @@ $(eval $(call SetupExecute, sleef_cross_build, \
116116
INFO := Building cross-compiling sleef, \
117117
DEPS := $(sleef_cross_config), \
118118
OUTPUT_DIR := $(SLEEF_NATIVE_BUILD_DIR), \
119-
COMMAND := cd $(SLEEF_SOURCE_DIR) && $(CMAKE) --build \
120-
$(SLEEF_CROSS_BUILD_DIR) -j, \
119+
WORKING_DIR := $(SLEEF_SOURCE_DIR), \
120+
COMMAND := $(CMAKE) --build $(SLEEF_CROSS_BUILD_DIR) -j, \
121121
))
122122

123123
TARGETS := $(sleef_cross_build)

make/common/Execute.gmk

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ ifeq ($(INCLUDE), true)
4545
# e.g. a simple sed replacement on the input file. If the operations are
4646
# unrelated to the main COMMAND, this is not a suitable solution.
4747
#
48+
# Before execution, the current working directory is changed to SUPPORT_DIR.
49+
# This can be overridden with WORKING_DIR.
50+
#
4851
# If your command outputs a variety of files, or if it's really a single file
4952
# but you don't really care about the output from the perspective, you can just
5053
# supply an OUTPUT_DIR. You are supposed to make sure the command creates files
@@ -75,6 +78,7 @@ ifeq ($(INCLUDE), true)
7578
# OUTPUT_DIR : The directory that will contain the result from the command
7679
# OUTPUT_FILE : Use this if the command results in a single output file
7780
# SUPPORT_DIR : Where to store generated support files
81+
# WORKING_DIR : Directory to cd to before executing the command
7882
# INFO : Message to display at LOG=info level when running command (optional)
7983
# WARN : Message to display at LOG=warn level when running command (optional)
8084
# DEPS : Dependencies for the execution to take place
@@ -133,6 +137,10 @@ define SetupExecuteBody
133137

134138
endif
135139

140+
ifeq ($$($1_WORKING_DIR), )
141+
$1_WORKING_DIR := $$($1_SUPPORT_DIR)
142+
endif
143+
136144
ifeq ($$($1_INFO)$$($1_WARN), )
137145
# If neither info nor warn is provided, add basic info text.
138146
$1_INFO := Running commands for $1
@@ -147,14 +155,14 @@ define SetupExecuteBody
147155
ifneq ($$($1_INFO), )
148156
$$(call LogInfo, $$($1_INFO))
149157
endif
150-
$$(call MakeDir, $$($1_SUPPORT_DIR) $$($1_OUTPUT_DIR))
158+
$$(call MakeDir, $$(call EncodeSpace, $$($1_WORKING_DIR)) $$(call EncodeSpace, $$($1_SUPPORT_DIR)) $$(call EncodeSpace, $$($1_OUTPUT_DIR)))
151159
$$(call ExecuteWithLog, $$($1_BASE)_pre, \
152-
$$($1_PRE_COMMAND))
160+
cd $$($1_WORKING_DIR) && $$($1_PRE_COMMAND))
153161
$$(TOUCH) $$@
154162

155163
$$($1_EXEC_RESULT): $$($1_PRE_MARKER)
156164
$$(call ExecuteWithLog, $$($1_BASE)_exec, \
157-
$$($1_COMMAND))
165+
cd $$($1_WORKING_DIR) && $$($1_COMMAND))
158166
ifeq ($$($1_EXEC_RESULT), $$($1_EXEC_MARKER))
159167
$$(TOUCH) $$@
160168
endif
@@ -168,9 +176,9 @@ define SetupExecuteBody
168176
ifneq ($$($1_INFO), )
169177
$$(call LogInfo, $$($1_INFO))
170178
endif
171-
$$(call MakeDir, $$(call EncodeSpace, $$($1_SUPPORT_DIR)) $$(call EncodeSpace, $$($1_OUTPUT_DIR)))
179+
$$(call MakeDir, $$(call EncodeSpace, $$($1_WORKING_DIR)) $$(call EncodeSpace, $$($1_SUPPORT_DIR)) $$(call EncodeSpace, $$($1_OUTPUT_DIR)))
172180
$$(call ExecuteWithLog, $$($1_BASE)_exec, \
173-
$$($1_COMMAND))
181+
cd $$($1_WORKING_DIR) && $$($1_COMMAND))
174182
ifeq ($$($1_EXEC_RESULT), $$($1_EXEC_MARKER))
175183
$$(TOUCH) $$@
176184
endif
@@ -182,7 +190,7 @@ define SetupExecuteBody
182190

183191
$$($1_FINAL_RESULT): $$($1_EXEC_RESULT)
184192
$$(call ExecuteWithLog, $$($1_BASE)_post, \
185-
$$($1_POST_COMMAND))
193+
cd $$($1_WORKING_DIR) && $$($1_POST_COMMAND))
186194
$$(TOUCH) $$@
187195

188196
$1 += $$($1_FINAL_RESULT)

test/make/TestExecute.gmk

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#
2+
# Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved.
3+
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
#
5+
# This code is free software; you can redistribute it and/or modify it
6+
# under the terms of the GNU General Public License version 2 only, as
7+
# published by the Free Software Foundation.
8+
#
9+
# This code is distributed in the hope that it will be useful, but WITHOUT
10+
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
# version 2 for more details (a copy is included in the LICENSE file that
13+
# accompanied this code).
14+
#
15+
# You should have received a copy of the GNU General Public License version
16+
# 2 along with this work; if not, write to the Free Software Foundation,
17+
# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
#
19+
# Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
# or visit www.oracle.com if you need additional information or have any
21+
# questions.
22+
#
23+
24+
include MakeFileStart.gmk
25+
26+
################################################################################
27+
28+
include Execute.gmk
29+
include UtilsForTests.gmk
30+
31+
THIS_FILE := $(TOPDIR)/test/make/TestExecute.gmk
32+
DEPS := $(THIS_FILE) \
33+
$(TOPDIR)/make/common/MakeBase.gmk \
34+
#
35+
36+
OUTPUT_DIR := $(TESTMAKE_OUTPUTDIR)/execute
37+
$(call MakeDir, $(OUTPUT_DIR))
38+
39+
################################################################################
40+
# Test SetupExecute
41+
42+
$(eval $(call SetupExecute, EXEC_1, \
43+
INFO := Testing that SetupExecute runs from SUPPORT_DIR, \
44+
OUTPUT_DIR := $(OUTPUT_DIR)/exec_1, \
45+
SUPPORT_DIR := $(OUTPUT_DIR)/exec_1/support, \
46+
COMMAND := $(ECHO) "Generating junk file" > ./junkfile, \
47+
))
48+
49+
run-test1: $(EXEC_1)
50+
test -f $(OUTPUT_DIR)/exec_1/support/junkfile
51+
52+
$(eval $(call SetupExecute, EXEC_2, \
53+
INFO := Testing that SetupExecute runs from SUPPORT_DIR, \
54+
OUTPUT_DIR := $(OUTPUT_DIR)/exec_2, \
55+
SUPPORT_DIR := $(OUTPUT_DIR)/exec_2/support, \
56+
WORKING_DIR := $(OUTPUT_DIR)/exec_2/special, \
57+
COMMAND := $(ECHO) "Generating special file" > ./specialfile, \
58+
))
59+
60+
run-test2: $(EXEC_2)
61+
test -f $(OUTPUT_DIR)/exec_2/special/specialfile
62+
63+
64+
TEST_TARGETS += run-test1 run-test2
65+
66+
.PHONY: run-test1 run-test2
67+
68+
################################################################################
69+
70+
all: $(TEST_TARGETS)
71+
72+
################################################################################
73+
74+
include MakeFileEnd.gmk

test/make/TestMake.gmk

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ java-compilation:
3434
copy-files:
3535
+$(MAKE) -f TestCopyFiles.gmk $(TEST_SUBTARGET)
3636

37+
execute:
38+
+$(MAKE) -f TestExecute.gmk $(TEST_SUBTARGET)
39+
3740
fix-deps-file:
3841
+$(MAKE) -f TestFixDepsFile.gmk $(TEST_SUBTARGET)
3942

@@ -47,7 +50,7 @@ configure:
4750
$(BASH) $(TOPDIR)/test/make/autoconf/test-configure.sh \
4851
"$(AUTOCONF)" "$(TOPDIR)" "$(TEST_SUPPORT_DIR)"
4952

50-
TARGETS += make-base java-compilation copy-files fix-deps-file idea \
53+
TARGETS += make-base java-compilation copy-files execute fix-deps-file idea \
5154
compile-commands configure
5255

5356
# Prints targets to TARGETS_FILE which must be set when calling this target.

0 commit comments

Comments
 (0)