Skip to content

Instantly share code, notes, and snippets.

@markbenvenuto
Created October 22, 2019 17:52
Show Gist options
  • Save markbenvenuto/1d23f6e6877816b9f2687158e41919cd to your computer and use it in GitHub Desktop.
Save markbenvenuto/1d23f6e6877816b9f2687158e41919cd to your computer and use it in GitHub Desktop.
Generate compile_commands.json on MacOS with Clang
#!/usr/bin/env python3
# Configures the build to use XCode targeting macOS
import json
import os
import os.path
import string
import subprocess
import sys
def exit_with_error(err_str):
"""print error in red and exit."""
print("\033[91mERROR: %s\033[0m" % (err_str))
sys.exit(1)
def print_ok(err_str):
"""print ok message in green."""
print("\033[92m%s\033[0m" % (err_str))
def setup_icecream():
"""Do setup"""
brew_cmd = "/usr/local/bin/brew"
# Step 1 - check for brew
if not os.path.exists(brew_cmd):
exit_with_error("Homebrew was not found at %s, please install homebrew at http://brew.sh" % (brew_cmd))
# Step 2 - check version of icecream
try:
icecream_js = subprocess.check_output([brew_cmd, "info", "--json=v1", "llvm"]).decode('utf-8')
except subprocess.CalledProcessError:
exit_with_error("icecream is not installed, please install icecream with 'brew install llvm'")
# Step 3 - Find the version
icecc_js = json.loads(icecream_js)
# Step 3.1 - Try grabbing linked_key first.
# If icecream is not linked, it comes back as "null" from brew, without the quotes
# TODO - verify how versions sort
icecream_version = icecc_js[0]["installed"][0]["version"]
#CC = subprocess.check_output(['xcrun', '-f', '--sdk', 'macosx', 'clang']).decode('utf-8').strip()
#CXX = subprocess.check_output(['xcrun', '-f', '--sdk', 'macosx', 'clang++']).decode('utf-8').strip()
CC = "/usr/local/Cellar/llvm/{}/bin/clang".format(icecream_version)
CXX = "/usr/local/Cellar/llvm/{}/bin/clang++".format(icecream_version)
sdk_path = subprocess.check_output(['xcrun', '--sdk', 'macosx', '--show-sdk-path']).decode('utf-8').strip()
# TODO - verify c++ include dir path
CCFLAGS = "-isysroot {} -isystem /usr/local/Cellar/llvm/{}/include/c++/v1 -mmacosx-version-min=10.12 -target darwin16.0.0 -arch x86_64".format(sdk_path, icecream_version)
subprocess.check_call(["python3", "buildscripts/scons.py", "CC="+CC, "CXX="+CXX, "CCFLAGS="+CCFLAGS, "compiledb", "generated-sources"])
print_ok("Icecream setup with launchctl complete. Happy Building!")
print("\nIcecream will now be run as the current user on login.")
print("To stop, run launchctl stop com.mongodb.iceccd")
return
if __name__ == "__main__":
setup_icecream()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment