Skip to content

Commit

Permalink
Simplify Xcode CI / Docs (mozilla#2266)
Browse files Browse the repository at this point in the history
* Add `objective-c++-header` as supported language.

* Parse -ivfsstatcache

* Simplify Xcode CI / Docs

I happened to stumble upon the `C_COMPILER_LAUNCHER` Xcode attribute,
which fits the bill perfectly, so use that instead.
  • Loading branch information
scoopr authored Oct 7, 2024
1 parent af5ea09 commit 2932b22
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 53 deletions.
13 changes: 0 additions & 13 deletions .github/workflows/integration-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -912,19 +912,6 @@ jobs:
run: |
cargo build
# Wrapper for the compiler invocation, so that we have a single command
# that calls sccache with the actual compiler argument.
# Just setting CC to be `sccache clang++` doesn't work as it is
# interpreted as the whole command that just happens to contain spaces.
- name: Create wrapper script
working-directory: tests/xcode
run: |
echo '#!/bin/sh' > wrapper.sh
echo "$GITHUB_WORKSPACE/${SCCACHE_PATH} $(xcrun -f cc) \$@" >> wrapper.sh
chmod +x wrapper.sh
echo "Wrapper script:"
cat wrapper.sh
- name: Start server
run: ${SCCACHE_PATH} --start-server

Expand Down
56 changes: 19 additions & 37 deletions docs/Xcode.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,27 +44,25 @@ Or by setting it up in a `launchd` configuration, perhaps as `~/Library/LaunchAg
```

### Setting it up for `xcodebuild`
When you override the `CC` variable for `xcodebuild`, it seems to always escape the spaces, so its not enough to just set it, but we need a wrapper script, something like

```sh
echo "#\!/bin/sh\nsccache $(xcrun -f cc) \$@" > wrapper.sh
chmod +x wrapper.sh
```
(YMMV if you need to select another sdk or toolchain for the xcrun)
Xcode seems to support barely documented `C_COMPILER_LAUNCHER` attribute, for
having a custom launcher program.

Then you can invoke `xcodebuild` like so
```sh
xcodebuild CC="$(pwd)/wrapper.sh"
xcodebuild C_COMPILER_LAUNCHER=sccache
CLANG_ENABLE_MODULES=NO
COMPILER_INDEX_STORE_ENABLE=NO
CLANG_USE_RESPONSE_FILE=NO
```
Where the additional arguments are for disabling some features that `sccache` can't cache currently.

These build settings can also be put in a xcconfig file, like `sccache.xcconfig`
```
CC=$(SRCROOT)/wrapper.sh
C_COMPILER_LAUNCHER=sccache
CLANG_ENABLE_MODULES=NO
COMPILER_INDEX_STORE_ENABLE=NO
CLANG_USE_RESPONSE_FILE=NO
```
Which can then be invoked with
```sh
Expand All @@ -75,39 +73,23 @@ xcodebuild -xcconfig sccache.xcconfig
### Setting it up for `cmake` Xcode generator
While `cmake` has the convenient `CMAKE_<LANG>_COMPILER_LAUNCHER` for prepending tools like `sccache`, it is not supported for the Xcode generator.

It can be then integrated with having a template file for the wrapper script, `launcher.sh.in`:
```sh
#!/bin/sh
exec "${CCACHE_EXE}" "${LAUNCHER_COMPILER}" "$@"
```

And then configuring it something like
But you can configuring it directly with something like
```cmake
# This bit before the first `project()`, as the COMPILER_LAUNCHER variables are read in then
if(DEFINED CCACHE)
find_program(CCACHE_EXE ${CCACHE} REQUIRED)
if(NOT CMAKE_GENERATOR STREQUAL "Xcode")
# Support for other generators should work with these
set(CMAKE_C_COMPILER_LAUNCHER "${CCACHE_EXE}")
set(CMAKE_CXX_COMPILER_LAUNCHER "${CCACHE_EXE}")
endif()
endif()
# .. your project stuff ..
# This bit needs to be after the first `project()` call, to have valid `CMAKE_C_COMPILER` variable.
# Alternatively in a file included with CMAKE_PROJECT_INCLUDE
if(DEFINED CCACHE)
if(CMAKE_GENERATOR STREQUAL "Xcode")
set(LAUNCHER_COMPILER ${CMAKE_C_COMPILER})
configure_file(${CMAKE_CURRENT_LIST_DIR}/launcher.sh.in launcher-cc.sh)
execute_process(COMMAND chmod a+rx
"${CMAKE_CURRENT_BINARY_DIR}/launcher-cc.sh")
set(CMAKE_XCODE_ATTRIBUTE_CC "${CMAKE_CURRENT_BINARY_DIR}/launcher-cc.sh")
set(CMAKE_XCODE_ATTRIBUTE_CLANG_ENABLE_MODULES "NO")
set(CMAKE_XCODE_ATTRIBUTE_COMPILER_INDEX_STORE_ENABLE "NO")
endif()
find_program(CCACHE_EXE ${CCACHE} REQUIRED)
if(NOT CMAKE_GENERATOR STREQUAL "Xcode")
# Support for other generators should work with these
set(CMAKE_C_COMPILER_LAUNCHER "${CCACHE_EXE}")
set(CMAKE_CXX_COMPILER_LAUNCHER "${CCACHE_EXE}")
else()
# And this should work for Xcode generator
set(CMAKE_XCODE_ATTRIBUTE_C_COMPILER_LAUNCHER ${CCACHE_EXE})
set(CMAKE_XCODE_ATTRIBUTE_CLANG_ENABLE_MODULES "NO")
set(CMAKE_XCODE_ATTRIBUTE_COMPILER_INDEX_STORE_ENABLE "NO")
set(CMAKE_XCODE_ATTRIBUTE_CLANG_USE_RESPONSE_FILE "NO")
endif()
endif()
```
Then configuring with `-DCCACHE=sccache` should work on all generators.
Expand Down
6 changes: 4 additions & 2 deletions src/compiler/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ pub enum Language {
CxxHeader,
ObjectiveC,
ObjectiveCxx,
ObjectiveCxxHeader,
Cuda,
Rust,
Hip,
Expand Down Expand Up @@ -152,7 +153,7 @@ impl Language {
Language::CxxHeader => "c++Header",
Language::GenericHeader => "c/c++",
Language::ObjectiveC => "objc",
Language::ObjectiveCxx => "objc++",
Language::ObjectiveCxx | Language::ObjectiveCxxHeader => "objc++",
Language::Cuda => "cuda",
Language::Rust => "rust",
Language::Hip => "hip",
Expand All @@ -169,7 +170,8 @@ impl CompilerKind {
| Language::CxxHeader
| Language::GenericHeader
| Language::ObjectiveC
| Language::ObjectiveCxx => "C/C++",
| Language::ObjectiveCxx
| Language::ObjectiveCxxHeader => "C/C++",
Language::Cuda => "CUDA",
Language::Rust => "Rust",
Language::Hip => "HIP",
Expand Down
3 changes: 3 additions & 0 deletions src/compiler/gcc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ counted_array!(pub static ARGS: [ArgInfo<ArgData>; _] = [
take_arg!("-iquote", PathBuf, CanBeSeparated, PreprocessorArgumentPath),
take_arg!("-isysroot", PathBuf, CanBeSeparated, PreprocessorArgumentPath),
take_arg!("-isystem", PathBuf, CanBeSeparated, PreprocessorArgumentPath),
take_arg!("-ivfsstatcache", PathBuf, CanBeSeparated, PassThroughPath),
take_arg!("-iwithprefix", PathBuf, CanBeSeparated, PreprocessorArgumentPath),
take_arg!("-iwithprefixbefore", PathBuf, CanBeSeparated, PreprocessorArgumentPath),
flag!("-nostdinc", PreprocessorArgumentFlag),
Expand Down Expand Up @@ -387,6 +388,7 @@ where
"c++-header" => Some(Language::CxxHeader),
"objective-c" => Some(Language::ObjectiveC),
"objective-c++" => Some(Language::ObjectiveCxx),
"objective-c++-header" => Some(Language::ObjectiveCxxHeader),
"cu" => Some(Language::Cuda),
"rs" => Some(Language::Rust),
"cuda" => Some(Language::Cuda),
Expand Down Expand Up @@ -678,6 +680,7 @@ fn language_to_gcc_arg(lang: Language) -> Option<&'static str> {
Language::CxxHeader => Some("c++-header"),
Language::ObjectiveC => Some("objective-c"),
Language::ObjectiveCxx => Some("objective-c++"),
Language::ObjectiveCxxHeader => Some("objective-c++-header"),
Language::Cuda => Some("cu"),
Language::Rust => None, // Let the compiler decide
Language::Hip => Some("hip"),
Expand Down
3 changes: 2 additions & 1 deletion tests/xcode/sccache.xcconfig
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
CC=$(SRCROOT)/wrapper.sh
C_COMPILER_LAUNCHER=../../target/debug/sccache
CLANG_ENABLE_MODULES=NO
COMPILER_INDEX_STORE_ENABLE=NO
CLANG_USE_RESPONSE_FILE=NO

0 comments on commit 2932b22

Please sign in to comment.