Skip to content

Commit 7a1a41f

Browse files
authored
Merge pull request #8290 from apple/revert-8268-swiftenv-fixes
Revert "swiftenv-script: Fix some issues with swiftenvs."
2 parents 204030a + e480227 commit 7a1a41f

File tree

5 files changed

+111
-29
lines changed

5 files changed

+111
-29
lines changed

utils/build-script

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -611,6 +611,8 @@ class BuildScriptInvocation(object):
611611
"--darwin-deployment-version-watchos=%s" % (
612612
args.darwin_deployment_version_watchos),
613613
"--cmake", toolchain.cmake,
614+
"--cmake-c-compiler", toolchain.cc,
615+
"--cmake-cxx-compiler", toolchain.cxx,
614616
"--cmark-build-type", args.cmark_build_variant,
615617
"--llvm-build-type", args.llvm_build_variant,
616618
"--swift-build-type", args.swift_build_variant,
@@ -1060,6 +1062,10 @@ def handle_swiftenv_args(args):
10601062
sys.exit(2) # 2 is the same as `argparse` error exit code.
10611063

10621064
if args.swiftenv_path is not None:
1065+
# mkdir the swiftenv_path
1066+
if not os.path.exists(args.swiftenv_path):
1067+
os.makedirs(args.swiftenv_path)
1068+
10631069
# Only use swiftenv_make/swiftenv_script if one is provided
10641070
# utils/swiftenv-make is default
10651071
if args.swiftenv_make is None:
@@ -2040,6 +2046,19 @@ iterations with -O",
20402046
"directory.".format(android.adb.commands.DEVICE_TEMP_DIR),
20412047
default=android.adb.commands.DEVICE_TEMP_DIR,
20422048
metavar="PATH")
2049+
2050+
parser.add_argument(
2051+
"--cmake-c-compiler",
2052+
help="the absolute path to CC, the 'clang' compiler for the host "
2053+
"platform. Default is auto detected.",
2054+
type=arguments.type.executable,
2055+
metavar="PATH")
2056+
parser.add_argument(
2057+
"--cmake-cxx-compiler",
2058+
help="the absolute path to CXX, the 'clang++' compiler for the host "
2059+
"platform. Default is auto detected.",
2060+
type=arguments.type.executable,
2061+
metavar="PATH")
20432062
parser.add_argument(
20442063
"--swiftenv-path",
20452064
help="the absolute path to a directory containing replacement compiler commands")
@@ -2228,6 +2247,19 @@ iterations with -O",
22282247
# Abstracted swiftenv_args for --preset and not
22292248
handle_swiftenv_args(args)
22302249

2250+
# Let args.cmake_c_compiler win over swiftenv's cc/cxx
2251+
if args.cmake_c_compiler is None and args.swiftenv_path is not None:
2252+
if os.path.exists(args.swiftenv_path + "/clang"):
2253+
args.cmake_c_compiler = args.swiftenv_path + "/clang"
2254+
if args.cmake_cxx_compiler is None and args.swiftenv_path is not None:
2255+
if os.path.exists(args.swiftenv_path + "/clang++"):
2256+
args.cmake_cxx_compiler = args.swiftenv_path + "/clang++"
2257+
2258+
if args.cmake_c_compiler is not None:
2259+
toolchain.cc = args.cmake_c_compiler
2260+
if args.cmake_cxx_compiler is not None:
2261+
toolchain.cxx = args.cmake_cxx_compiler
2262+
22312263
if args.cmake is not None:
22322264
toolchain.cmake = args.cmake
22332265

utils/build-script-impl

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1081,15 +1081,15 @@ check_swiftenv_args
10811081
# If we are using a swiftenv, do this next block
10821082
if [ "${SWIFTENV_PATH}" ]; then
10831083

1084-
if [ ! -d "${SWIFTENV_PATH}" ]; then
1084+
if [[ ! -d "${SWIFTENV_PATH}" ]]; then
10851085
echo "${COMMAND_NAME}: SWIFTENV_PATH does not exist, it will be created"
10861086
fi
10871087
if [[ "${SWIFTENV_RECREATE}" ]]; then
10881088
echo "${COMMAND_NAME}: SWIFTENV_PATH will be recreated because SWIFTENV_RECREATE is true"
10891089
fi
10901090

10911091
# redo the swiftenv by deleting, mkdir, and running the SWIFTENV_MAKE script
1092-
if [ ! -d "${SWIFTENV_PATH}" ] || [[ "${SWIFTENV_RECREATE}" ]]; then
1092+
if [[ ! -d "${SWIFTENV_PATH}" ]] || [[ "${SWIFTENV_RECREATE}" ]]; then
10931093
if [ ! -f "${SWIFTENV_MAKE}" ]; then
10941094
echo "${COMMAND_NAME} error: SWIFTENV_MAKE is not set."
10951095
exit 2
@@ -1099,10 +1099,17 @@ if [ "${SWIFTENV_PATH}" ]; then
10991099
exit 2
11001100
fi
11011101

1102-
call rm -rf "${SWIFTENV_PATH}"
1103-
call mkdir -p "${SWIFTENV_PATH}"
1104-
echo "${COMMAND_NAME}: Calling \"${SWIFTENV_MAKE}\" \"${SWIFTENV_PATH}\" \"${SWIFTENV_SCRIPT}\""
1105-
call "${SWIFTENV_MAKE}" "${SWIFTENV_PATH}" "${SWIFTENV_SCRIPT}"
1102+
if [[ -d "${SWIFTENV_PATH}" ]] && [[ "${SWIFTENV_RECREATE}" ]]; then
1103+
echo "${COMMAND_NAME}: SWIFTENV_PATH does exists, it will be deleted and recreated"
1104+
call rm -rf "${SWIFTENV_PATH}"
1105+
echo "${COMMAND_NAME}: Calling mkdir on SWIFTENV_PATH: ${SWIFTENV_PATH}"
1106+
call mkdir -p "${SWIFTENV_PATH}"
1107+
echo "${COMMAND_NAME}: Calling \"${SWIFTENV_MAKE}\" \"${SWIFTENV_PATH}\" \"${SWIFTENV_SCRIPT}\""
1108+
call "${SWIFTENV_MAKE}" "${SWIFTENV_PATH}" "${SWIFTENV_SCRIPT}"
1109+
fi
1110+
1111+
else
1112+
echo "${COMMAND_NAME}: SWIFTENV_PATH exists, not recreating the SWIFTENV"
11061113
fi
11071114
fi
11081115

@@ -1179,10 +1186,8 @@ function set_cmake_tools {
11791186
if [ "${CMAKE_C_COMPILER}" ]; then cmake_options=( "${cmake_options[@]}" -DCMAKE_C_COMPILER:PATH="${CMAKE_C_COMPILER}"); fi
11801187
if [ "${CMAKE_CXX_COMPILER}" ]; then cmake_options=( "${cmake_options[@]}" -DCMAKE_CXX_COMPILER:PATH="${CMAKE_CXX_COMPILER}"); fi
11811188
if [ "${CMAKE_AR}" ]; then cmake_options=( "${cmake_options[@]}" -DCMAKE_AR:PATH="${CMAKE_AR}"); fi
1182-
if [ "${CMAKE_LD}" ]; then cmake_options=( "${cmake_options[@]}" -DCMAKE_LD:PATH="${CMAKE_LD}"); fi
11831189
if [ "${CMAKE_RANLIB}" ]; then cmake_options=( "${cmake_options[@]}" -DCMAKE_RANLIB:PATH="${CMAKE_RANLIB}"); fi
11841190
if [ "${CMAKE_LIPO}" ]; then cmake_options=( "${cmake_options[@]}" -DCMAKE_LIPO:PATH="${CMAKE_LIPO}"); fi
1185-
if [ "${CMAKE_LIBTOOL}" ]; then cmake_options=( "${cmake_options[@]}" -DCMAKE_LIBTOOL:PATH="${CMAKE_LIBTOOL}"); fi
11861191
if [ "${CMAKE_INSTALL_NAME_TOOL}" ]; then cmake_options=( "${cmake_options[@]}" -DCMAKE_INSTALL_NAME_TOOL:PATH="${CMAKE_INSTALL_NAME_TOOL}"); fi
11871192
if [ "${CMAKE_CODESIGN}" ]; then cmake_options=( "${cmake_options[@]}" -DCMAKE_CODESIGN:PATH="${CMAKE_CODESIGN}"); fi
11881193
if [ "${CMAKE_PYTHON}" ]; then cmake_options=( "${cmake_options[@]}" -DPYTHON_EXECUTABLE:PATH="${CMAKE_PYTHON}"); fi
@@ -2107,7 +2112,12 @@ for host in "${ALL_HOSTS[@]}"; do
21072112
build_targets=(llvm-tblgen clang-headers)
21082113
fi
21092114

2110-
set_cmake_tools
2115+
if [ "${CMAKE_LIBTOOL}" ] ; then
2116+
cmake_options=(
2117+
"${cmake_options[@]}"
2118+
-DCMAKE_LIBTOOL:PATH="${CMAKE_LIBTOOL}"
2119+
)
2120+
fi
21112121

21122122
# Note: we set the variable:
21132123
#

utils/swift_build_support/swift_build_support/cmake.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,15 @@ def common_options(self):
9898
if args.export_compile_commands:
9999
define("CMAKE_EXPORT_COMPILE_COMMANDS", "ON")
100100

101+
if args.distcc:
102+
define("CMAKE_C_COMPILER:PATH", toolchain.distcc)
103+
define("CMAKE_C_COMPILER_ARG1", toolchain.cc)
104+
define("CMAKE_CXX_COMPILER:PATH", toolchain.distcc)
105+
define("CMAKE_CXX_COMPILER_ARG1", toolchain.cxx)
106+
else:
107+
define("CMAKE_C_COMPILER:PATH", toolchain.cc)
108+
define("CMAKE_CXX_COMPILER:PATH", toolchain.cxx)
109+
101110
if args.cmake_generator == 'Xcode':
102111
define("CMAKE_CONFIGURATION_TYPES",
103112
"Debug;Release;MinSizeRel;RelWithDebInfo")

utils/swift_build_support/tests/test_cmake.py

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ def mock_distcc_path(self):
2828
def default_args(self):
2929
"""Return new args object with default values
3030
"""
31-
return Namespace(enable_asan=False,
31+
return Namespace(cmake_c_compiler="/path/to/clang",
32+
cmake_cxx_compiler="/path/to/clang++",
33+
enable_asan=False,
3234
enable_ubsan=False,
3335
enable_tsan=False,
3436
export_compile_commands=False,
@@ -52,6 +54,8 @@ def cmake(self, args):
5254
"""Return new CMake object initialized with given args
5355
"""
5456
toolchain = host_toolchain()
57+
toolchain.cc = args.cmake_c_compiler
58+
toolchain.cxx = args.cmake_cxx_compiler
5559
if args.distcc:
5660
toolchain.distcc = self.mock_distcc_path()
5761
toolchain.ninja = self.which_ninja(args)
@@ -63,6 +67,8 @@ def test_common_options_defaults(self):
6367
self.assertEqual(
6468
list(cmake.common_options()),
6569
["-G", "Ninja",
70+
"-DCMAKE_C_COMPILER:PATH=/path/to/clang",
71+
"-DCMAKE_CXX_COMPILER:PATH=/path/to/clang++",
6672
"-DCMAKE_MAKE_PROGRAM=" + self.which_ninja(args)])
6773

6874
def test_common_options_asan(self):
@@ -73,6 +79,8 @@ def test_common_options_asan(self):
7379
list(cmake.common_options()),
7480
["-G", "Ninja",
7581
"-DLLVM_USE_SANITIZER=Address",
82+
"-DCMAKE_C_COMPILER:PATH=/path/to/clang",
83+
"-DCMAKE_CXX_COMPILER:PATH=/path/to/clang++",
7684
"-DCMAKE_MAKE_PROGRAM=" + self.which_ninja(args)])
7785

7886
def test_common_options_ubsan(self):
@@ -83,6 +91,8 @@ def test_common_options_ubsan(self):
8391
list(cmake.common_options()),
8492
["-G", "Ninja",
8593
"-DLLVM_USE_SANITIZER=Undefined",
94+
"-DCMAKE_C_COMPILER:PATH=/path/to/clang",
95+
"-DCMAKE_CXX_COMPILER:PATH=/path/to/clang++",
8696
"-DCMAKE_MAKE_PROGRAM=" + self.which_ninja(args)])
8797

8898
def test_common_options_tsan(self):
@@ -93,6 +103,8 @@ def test_common_options_tsan(self):
93103
list(cmake.common_options()),
94104
["-G", "Ninja",
95105
"-DLLVM_USE_SANITIZER=Thread",
106+
"-DCMAKE_C_COMPILER:PATH=/path/to/clang",
107+
"-DCMAKE_CXX_COMPILER:PATH=/path/to/clang++",
96108
"-DCMAKE_MAKE_PROGRAM=" + self.which_ninja(args)])
97109

98110
def test_common_options_asan_ubsan(self):
@@ -104,6 +116,8 @@ def test_common_options_asan_ubsan(self):
104116
list(cmake.common_options()),
105117
["-G", "Ninja",
106118
"-DLLVM_USE_SANITIZER=Address;Undefined",
119+
"-DCMAKE_C_COMPILER:PATH=/path/to/clang",
120+
"-DCMAKE_CXX_COMPILER:PATH=/path/to/clang++",
107121
"-DCMAKE_MAKE_PROGRAM=" + self.which_ninja(args)])
108122

109123
def test_common_options_ubsan_tsan(self):
@@ -115,6 +129,8 @@ def test_common_options_ubsan_tsan(self):
115129
list(cmake.common_options()),
116130
["-G", "Ninja",
117131
"-DLLVM_USE_SANITIZER=Undefined;Thread",
132+
"-DCMAKE_C_COMPILER:PATH=/path/to/clang",
133+
"-DCMAKE_CXX_COMPILER:PATH=/path/to/clang++",
118134
"-DCMAKE_MAKE_PROGRAM=" + self.which_ninja(args)])
119135

120136
def test_common_options_asan_ubsan_tsan(self):
@@ -127,6 +143,8 @@ def test_common_options_asan_ubsan_tsan(self):
127143
list(cmake.common_options()),
128144
["-G", "Ninja",
129145
"-DLLVM_USE_SANITIZER=Address;Undefined;Thread",
146+
"-DCMAKE_C_COMPILER:PATH=/path/to/clang",
147+
"-DCMAKE_CXX_COMPILER:PATH=/path/to/clang++",
130148
"-DCMAKE_MAKE_PROGRAM=" + self.which_ninja(args)])
131149

132150
def test_common_options_export_compile_commands(self):
@@ -137,6 +155,8 @@ def test_common_options_export_compile_commands(self):
137155
list(cmake.common_options()),
138156
["-G", "Ninja",
139157
"-DCMAKE_EXPORT_COMPILE_COMMANDS=ON",
158+
"-DCMAKE_C_COMPILER:PATH=/path/to/clang",
159+
"-DCMAKE_CXX_COMPILER:PATH=/path/to/clang++",
140160
"-DCMAKE_MAKE_PROGRAM=" + self.which_ninja(args)])
141161

142162
def test_common_options_distcc(self):
@@ -146,6 +166,10 @@ def test_common_options_distcc(self):
146166
self.assertEqual(
147167
list(cmake.common_options()),
148168
["-G", "Ninja",
169+
"-DCMAKE_C_COMPILER:PATH=" + self.mock_distcc_path(),
170+
"-DCMAKE_C_COMPILER_ARG1=/path/to/clang",
171+
"-DCMAKE_CXX_COMPILER:PATH=" + self.mock_distcc_path(),
172+
"-DCMAKE_CXX_COMPILER_ARG1=/path/to/clang++",
149173
"-DCMAKE_MAKE_PROGRAM=" + self.which_ninja(args)])
150174

151175
def test_common_options_xcode(self):
@@ -155,6 +179,8 @@ def test_common_options_xcode(self):
155179
self.assertEqual(
156180
list(cmake.common_options()),
157181
["-G", "Xcode",
182+
"-DCMAKE_C_COMPILER:PATH=/path/to/clang",
183+
"-DCMAKE_CXX_COMPILER:PATH=/path/to/clang++",
158184
"-DCMAKE_CONFIGURATION_TYPES=" +
159185
"Debug;Release;MinSizeRel;RelWithDebInfo"])
160186

@@ -167,6 +193,8 @@ def test_common_options_clang_compiler_version(self):
167193
self.assertEqual(
168194
list(cmake.common_options()),
169195
["-G", "Ninja",
196+
"-DCMAKE_C_COMPILER:PATH=/path/to/clang",
197+
"-DCMAKE_CXX_COMPILER:PATH=/path/to/clang++",
170198
"-DLLVM_VERSION_MAJOR:STRING=3",
171199
"-DLLVM_VERSION_MINOR:STRING=8",
172200
"-DLLVM_VERSION_PATCH:STRING=0",
@@ -179,6 +207,8 @@ def test_common_options_build_ninja(self):
179207
self.assertEqual(
180208
list(cmake.common_options()),
181209
["-G", "Ninja",
210+
"-DCMAKE_C_COMPILER:PATH=/path/to/clang",
211+
"-DCMAKE_CXX_COMPILER:PATH=/path/to/clang++",
182212
"-DCMAKE_MAKE_PROGRAM=" + self.which_ninja(args)])
183213

184214
def test_common_options_full(self):
@@ -198,6 +228,10 @@ def test_common_options_full(self):
198228
["-G", "Xcode",
199229
"-DLLVM_USE_SANITIZER=Address;Undefined",
200230
"-DCMAKE_EXPORT_COMPILE_COMMANDS=ON",
231+
"-DCMAKE_C_COMPILER:PATH=" + self.mock_distcc_path(),
232+
"-DCMAKE_C_COMPILER_ARG1=/path/to/clang",
233+
"-DCMAKE_CXX_COMPILER:PATH=" + self.mock_distcc_path(),
234+
"-DCMAKE_CXX_COMPILER_ARG1=/path/to/clang++",
201235
"-DCMAKE_CONFIGURATION_TYPES=" +
202236
"Debug;Release;MinSizeRel;RelWithDebInfo",
203237
"-DLLVM_VERSION_MAJOR:STRING=3",

utils/swiftenv-script

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ function find_real_command {
2727
fi
2828
case "$command_name" in
2929
clang|clang++|ld|swift|swiftc|lipo|ar|codesign|dsymutil|libtool|ranlib|strip|llvm-tblgen|python) "${find_util}" "${command_name}";;
30-
*) echo "$1";;
30+
*) echo "${command_name}";;
3131
esac
3232
}
3333
function find_o_target { # last -o wins, can short circuit for faster compiles
@@ -41,35 +41,32 @@ function find_o_target { # last -o wins, can short circuit for faster compiles
4141
esac; done
4242
echo "${o_target}"
4343
}
44-
function find_swift_targets {
44+
function find_module_target {
4545
while test $# -gt 0; do
4646
case "$1" in
47-
-o)
48-
local out="$2";
49-
outfiles=("${outfiles[@]}" "${out}")
50-
if [[ "$2" == *.swiftmodule ]]; then
51-
outfiles=("${outfiles[@]}" "${out:0:${#out}-12}.swiftdoc")
52-
fi
53-
shift; shift;;
54-
-output-file-map)
55-
outfiles=("${outfiles[@]}" "$2"); shift; shift;;
56-
-emit-module-path)
57-
local module_path="$2";
58-
outfiles=("${outfiles[@]}" "${module_path}")
59-
outfiles=("${outfiles[@]}" "${module_path:0:${#module_path}-12}.swiftdoc")
60-
shift; shift;;
61-
-emit-objc-header-path)
62-
outfiles=("${outfiles[@]}" "$2"); shift; shift;;
47+
-emit-module-path) echo "$2"; break;;
6348
*) shift;;
6449
esac; done
6550
}
51+
function emit_module_p { #emit -o's arg + swiftmodule
52+
while test $# -gt 0; do
53+
case "$1" in
54+
-emit-module) return 0;;
55+
*) shift;;
56+
esac; done; return 1
57+
}
6658
function find_command_targets { # sets: outfiles
6759
local name="$1"
6860
local args=("${@:2}")
6961
case "$name" in
7062
clang|clang++|ld|libtool|clang-tblgen|llvm-tblgen|lipo|gyb)
7163
outfiles=("${outfiles[@]}" $(find_o_target "${args[@]}"));;
72-
swift|swiftc) find_swift_targets "${args[@]}";;
64+
swift|swiftc)
65+
outfiles=("${outfiles[@]}" $(find_o_target "${args[@]}"));
66+
if emit_module_p "${args[@]}" ; then
67+
local last_outfile; last_outfile=("${outfiles[@]: -1}")
68+
outfiles=("${outfiles[@]}" "${last_outfile:0:${#last_outfile}-12}.swiftdoc"); fi
69+
find_module_target "${args[@]}";;
7370
ranlib) outfiles=("${outfiles[@]}" $(first "${args[@]}"));;
7471
ar) outfiles=("${outfiles[@]}" $(second "${args[@]}"));;
7572
codesign|dsymutil|install_name_tool|strip) outfiles=("${outfiles[@]}" $(last "${args[@]}"));;

0 commit comments

Comments
 (0)