-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[build-script] Determine HOST_CC in build-script #761
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
gribozavr
merged 1 commit into
swiftlang:master
from
modocache:sr-237-build-script-impl-merge-host-cc
Dec 27, 2015
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# swift_build_support | ||
|
||
`swift_build_support` is a Python module containing functions and data | ||
structures used by the Swift build script. | ||
|
||
You may run unit tests for `swift_build_support` from the command line: | ||
|
||
```sh | ||
apple/swift $ python -m unittest discover -s utils/swift_build_support | ||
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# swift_build_support/__init__.py - Helpers for building Swift -*- python -*- | ||
# | ||
# This source file is part of the Swift.org open source project | ||
# | ||
# Copyright (c) 2014 - 2015 Apple Inc. and the Swift project authors | ||
# Licensed under Apache License v2.0 with Runtime Library Exception | ||
# | ||
# See http://swift.org/LICENSE.txt for license information | ||
# See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors | ||
# | ||
# ---------------------------------------------------------------------------- | ||
# | ||
# This file needs to be here in order for Python to treat the | ||
# utils/swift_build_support/ directory as a module. | ||
# | ||
# ---------------------------------------------------------------------------- |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
# swift_build_support/clang.py - Detect host machine's Clang -*- python -*- | ||
# | ||
# This source file is part of the Swift.org open source project | ||
# | ||
# Copyright (c) 2014 - 2015 Apple Inc. and the Swift project authors | ||
# Licensed under Apache License v2.0 with Runtime Library Exception | ||
# | ||
# See http://swift.org/LICENSE.txt for license information | ||
# See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors | ||
# | ||
# ---------------------------------------------------------------------------- | ||
# | ||
# Find the path to a Clang executable on the host machine that is most | ||
# suitable for building Swift. | ||
# | ||
# ---------------------------------------------------------------------------- | ||
|
||
from __future__ import absolute_import | ||
|
||
import collections | ||
import platform | ||
import subprocess | ||
|
||
from . import xcrun | ||
from .which import which | ||
|
||
|
||
# A named tuple consisting of two paths: | ||
# 1. 'cc' is the path to a program used for compiling C. | ||
# 2. 'cxx' is the path to a program used for compiling C++. | ||
CompilerExecutable = collections.namedtuple('CompilerExecutable', 'cc cxx') | ||
|
||
|
||
def _freebsd_release_date(): | ||
""" | ||
Return the release date for FreeBSD operating system on this host. | ||
If the release date cannot be ascertained, return None. | ||
""" | ||
try: | ||
# For details on `sysctl`, see: | ||
# http://www.freebsd.org/cgi/man.cgi?sysctl(8) | ||
return int(subprocess.check_output( | ||
['sysctl', '-n', 'kern.osreldate']).rstrip()) | ||
except subprocess.CalledProcessError: | ||
return None | ||
|
||
|
||
def _first_clang(suffixes): | ||
""" | ||
Return a CompilerExecutable with the first available versions of clang | ||
and clang++, searching in the order of the given suffixes. | ||
|
||
If no Clang executables are found, return None. | ||
""" | ||
for suffix in suffixes: | ||
cc_path = which('clang{}'.format(suffix)) | ||
cxx_path = which('clang++{}'.format(suffix)) | ||
if cc_path and cxx_path: | ||
return CompilerExecutable(cc=cc_path, cxx=cxx_path) | ||
|
||
return None | ||
|
||
|
||
def host_clang(xcrun_toolchain): | ||
""" | ||
Return a CompilerExecutable for the host platform. | ||
If no appropriate compilers can be found, return None. | ||
""" | ||
if platform.system() == 'Darwin': | ||
cc = xcrun.find(xcrun_toolchain, 'clang') | ||
cxx = xcrun.find(xcrun_toolchain, 'clang++') | ||
if cc and cxx: | ||
return CompilerExecutable(cc=cc, cxx=cxx) | ||
else: | ||
return None | ||
elif platform.system() == 'FreeBSD': | ||
# See: https://github.com/apple/swift/pull/169 | ||
# Building Swift from source requires a recent version of the Clang | ||
# compiler with C++14 support. | ||
freebsd_release_date = _freebsd_release_date() | ||
if freebsd_release_date and freebsd_release_date >= 1100000: | ||
# On newer releases of FreeBSD, the default Clang is sufficient. | ||
return CompilerExecutable(cc='clang', cxx='clang++') | ||
else: | ||
# On older releases, or on releases for which we cannot determine | ||
# the release date, we search for the most modern version | ||
# available. | ||
return _first_clang(['38', '37', '36', '35']) | ||
else: | ||
return _first_clang(['', '-3.8', '-3.7', '-3.6', '-3.5']) |
56 changes: 56 additions & 0 deletions
56
utils/swift_build_support/swift_build_support/migration.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
# swift_build_support/migration.py - Migrating build-script -*- python -*- | ||
# | ||
# This source file is part of the Swift.org open source project | ||
# | ||
# Copyright (c) 2014 - 2015 Apple Inc. and the Swift project authors | ||
# Licensed under Apache License v2.0 with Runtime Library Exception | ||
# | ||
# See http://swift.org/LICENSE.txt for license information | ||
# See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors | ||
# | ||
# ---------------------------------------------------------------------------- | ||
# | ||
# utils/build-script takes arguments for its argument parser, as well as | ||
# arguments that are meant to be passed directly to utils/build-script-impl. | ||
# In order to gradually migrate away from build-script-impl, this module | ||
# provides tools to handle parsing of these args. | ||
# | ||
# ---------------------------------------------------------------------------- | ||
|
||
|
||
def migrate_impl_args(argv, migrate_args): | ||
""" | ||
Given a list of arguments of the form: | ||
|
||
--foo --bar=baz -- --flim=flam | ||
|
||
And a list of arguments to migrate, return a list in which the arguments | ||
to migrate come before the '--' separator. For example, were we to migrate | ||
'--flim', we would return: | ||
|
||
--foo --bar=baz --flim=flam -- | ||
|
||
Note that we do not attempt to remove the '--' separator if it is no longer | ||
necessary, nor do we replace '--flim' if it already appears before the | ||
separator. In these cases, argparse "does the right thing": it can handle | ||
a trailing separator, and when options that are specified twice argparse | ||
uses the second value. | ||
""" | ||
try: | ||
split_index = argv.index('--') | ||
except ValueError: | ||
# If there is no separator, then we have nothing to migrate. | ||
return argv | ||
|
||
args = argv[:split_index] | ||
impl_args = argv[split_index:] | ||
impl_args_to_remove = [] | ||
for index, impl_arg in enumerate(impl_args): | ||
if impl_arg.split('=')[0] in migrate_args: | ||
args.append(impl_arg) | ||
impl_args_to_remove.append(impl_arg) | ||
|
||
for impl_arg_to_remove in impl_args_to_remove: | ||
impl_args.remove(impl_arg_to_remove) | ||
|
||
return args + impl_args |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# swift_build_support/which.py - shutil.which() for Python 2.7 -*- python -*- | ||
# | ||
# This source file is part of the Swift.org open source project | ||
# | ||
# Copyright (c) 2014 - 2015 Apple Inc. and the Swift project authors | ||
# Licensed under Apache License v2.0 with Runtime Library Exception | ||
# | ||
# See http://swift.org/LICENSE.txt for license information | ||
# See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors | ||
# | ||
# ---------------------------------------------------------------------------- | ||
# | ||
# A naive reimplementation of shutil.which() for Python 2.7. This can be | ||
# removed if shutil.which() is backported, or if the Swift build toolchain | ||
# migrates completely to Python 3.3+. | ||
# | ||
# ---------------------------------------------------------------------------- | ||
|
||
import subprocess | ||
|
||
|
||
def which(cmd): | ||
""" | ||
Return the path to an executable which would be run if | ||
the given cmd was called. If no cmd would be called, return None. | ||
|
||
Python 3.3+ provides this behavior via the shutil.which() function; | ||
see: https://docs.python.org/3.3/library/shutil.html#shutil.which | ||
|
||
We provide our own implementation because shutil.which() has not | ||
been backported to Python 2.7, which we support. | ||
""" | ||
try: | ||
return subprocess.check_output(['which', cmd]).rstrip() | ||
except subprocess.CalledProcessError: | ||
return None |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# swift_build_support/xcrun.py - Invoke xcrun from Python -*- python -*- | ||
# | ||
# This source file is part of the Swift.org open source project | ||
# | ||
# Copyright (c) 2014 - 2015 Apple Inc. and the Swift project authors | ||
# Licensed under Apache License v2.0 with Runtime Library Exception | ||
# | ||
# See http://swift.org/LICENSE.txt for license information | ||
# See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors | ||
# | ||
# ---------------------------------------------------------------------------- | ||
# | ||
# Python wrappers for invoking `xcrun` on the command-line. | ||
# | ||
# ---------------------------------------------------------------------------- | ||
|
||
import subprocess | ||
|
||
|
||
def find(toolchain, tool): | ||
""" | ||
Return the path for the given tool, according to `xcrun --find`, using | ||
the given toolchain. If `xcrun --find` cannot find the tool, return None. | ||
""" | ||
try: | ||
# `xcrun --find` prints to stderr when it fails to find the | ||
# given tool. We swallow that output with a pipe. | ||
out = subprocess.check_output(['xcrun', '--sdk', 'macosx', | ||
'--toolchain', toolchain, | ||
'--find', tool], | ||
stderr=subprocess.PIPE) | ||
return out.rstrip() | ||
except subprocess.CalledProcessError: | ||
return None |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# swift_build_support/tests/__init__.py - Test module -*- python -*- | ||
# | ||
# This source file is part of the Swift.org open source project | ||
# | ||
# Copyright (c) 2014 - 2015 Apple Inc. and the Swift project authors | ||
# Licensed under Apache License v2.0 with Runtime Library Exception | ||
# | ||
# See http://swift.org/LICENSE.txt for license information | ||
# See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors | ||
# | ||
# ---------------------------------------------------------------------------- | ||
# | ||
# This file needs to be here in order for Python to treat the | ||
# utils/swift_build_support/tests/ directory as a module. | ||
# | ||
# ---------------------------------------------------------------------------- |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, but this is not a backwards-compatible change. Previously,
--darwin-xcrun-toolchain
was an argument that was passed after--
:In order for the argument parsing to catch it now, it needs to be passed before
--
:I'm totally on board with breaking backward compatibility once when the refactoring is done, but not continuously throughout the process.
Thus, for staging, it seems like what we need to do is to preprocess the command line like this:
--darwin-xcrun-toolchain.*
arguments from the command line into a separate list.--
.parser.parse_args()
.I think this pattern will appear over and over again as you migrate handling of other command-line arguments to Python, so it would make sense to make it a reusable function.