Skip to content

Instantly share code, notes, and snippets.

@etuttle
Last active June 28, 2020 20:49
Show Gist options
  • Save etuttle/b520fd2769410b70aed14a6ba8846a98 to your computer and use it in GitHub Desktop.
Save etuttle/b520fd2769410b70aed14a6ba8846a98 to your computer and use it in GitHub Desktop.
osx-build-shell.sh
#!/bin/bash
# The compiler toolchain is specified by `xcode-select`. You can use
# `/Library/Developer/CommandLineTools` selected as the Developer
# directory. That should make the build independent of Xcode.app.
#
# eg: sudo xcode-select -s /Library/Developer/CommandLineTools
#
# If you are on 10.14+, make sure your CLI tools are installed:
#
# * sudo installer -pkg \
# /Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkg \
# -target /
tools="$(xcode-select -p)"
repo=$(git rev-parse --show-toplevel)
repo_name="$(basename "$(git remote get-url origin)")"
repo_noext="${repo_name%.*}"
if [[ $tools == /Library/Developer/CommandLineTools* ]]; then
tools_path="${tools}/usr/bin"
sysroot_="${tools}/SDKs/MacOSX.sdk"
elif [[ $tools == /Applications/Xcode.app* ]]; then
echo >&2 " Using Xcode.app toolchain paths (untested!)."
tools_path="${tools}/Toolchains/XcodeDefault.xctoolchain/usr/bin"
sysroot_="${tools}/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk"
else
echo >&2 " $tools dir not recognized"
exit 1
fi
exp_() {
exp="${1}=\"${2}\""
echo $exp
eval "export $exp"
}
exp_ SYSROOT "$sysroot_"
exp_ CC "${tools_path}/clang"
exp_ CXX "${tools_path}/clang++"
exp_ CFLAGS "-std=c99 -stdlib=libc++ --sysroot=${SYSROOT} -g"
exp_ CXXFLAGS "-std=c++11 -stdlib=libc++ --sysroot=${SYSROOT} -g"
# comment out the following if you want the linker to fail on missing symbols
exp_ LDFLAGS "-Wl,-undefined -Wl,dynamic_lookup"
exp_ PATH "/usr/local/bin:${tools_path}:${PATH}"
if test -n "$repo_noext"; then
export PS1="OSXMAKE(${repo_noext}) \W\$ "
fi
run=("$SHELL")
# simple bash bytecode to pass code to the subshell rcfile :)
# add normal commands and arguments using add_rc arg1 aerg2 arg3
# you can also add shell metachars by prefixing \0, eg \0'2>&1'
# or add a newline to the rc using an escaped ;
rc=()
add_rc() { rc=( "${rc[@]}" "$@" ) ; }
echo_rc() { for w in "${rc[@]}"; do
if [[ "${w:0:1}" == \0 ]]; then echo -n ${w:1}" ";
elif [[ $w == ';' ]]; then echo;
else printf "%q " "$w";
fi;
done
}
repo=$(git rev-parse --show-toplevel)
venv="${repo}/.env/bin/activate"
if test -n $repo && test -e "$venv"; then
add_rc echo \0'2>&1' "Enabling virtualenv at ${venv}" \; source "$venv" \;
fi
if test -n $repo; then
repo_name="$(basename "$(git remote get-url origin)")"
repo_noext="${repo_name%.*}"
if test -n "$repo_noext"; then
case "$repo_noext" in
libmicrohttpd)
add_rc echo "To deal with libtool, configure to install in to private dir:" \;
add_rc echo "./configure --prefix=\$(pwd)/../libmicrohttpd-install" \;
add_rc echo \;
add_rc echo "Use this to rebuild the compile_commands.json for CLion" \;
add_rc echo " (avoids builing docs/ which requires a bunch of TeX tools):" \;
add_rc echo " compiledb --overwrite --full-path make -C src all" \;
;;
glewlwyd)
add_rc "cmake_opts=-DWITH_JOURNALD=off \
-DCMAKE_BUILD_TYPE=Debug \
-DCMAKE_POLICY_DEFAULT_CMP0079=NEW \
-DMHD_INCLUDE_DIR:PATH=${repo}/../libmicrohttpd-install/include \
-DMHD_LIBRARY:FILEPATH=${repo}/../libmicrohttpd-install/lib/libmicrohttpd.12.dylib \
-DMYSQL_INCLUDE_DIR:PATH=/usr/local/mysql/include \
-DMYSQL_LIBRARY:FILEPATH=/usr/local/mysql/lib/libmysqlclient.dylib \
-DSQLITE3_INCLUDE_DIR:PATH=/usr/local/opt/sqlite/include \
-DSQLITE3_LIBRARY_DEBUG:FILEPATH=/usr/local/opt/sqlite/lib/libsqlite3.dylib \
-DSQLITE3_LIBRARY_RELEASE:FILEPATH=/usr/local/opt/sqlite/lib/libsqlite3.dylib \
-DPG_CONFIG_EXECUTABLE:FILEPATH=/usr/local/opt/libpq/bin/pg_config \
-DLDAP_INCLUDE_DIR:PATH=/usr/local/opt/openldap/include \
-DLDAP_LIBRARY:FILEPATH=/usr/local/opt/openldap/lib/libldap.dylib" \;
add_rc echo " Call cmake like cmake \$cmake_opts" \;
add_rc echo " You can paste \$cmake_opts and shell ENV from above into CLion settings" \;
;;
*)
;;
esac
fi
fi
if [[ ${#rc[@]} -eq 0 ]]; then
"${run[@]}"
else
# cat <(echo_rc)
"${run[@]}" --rcfile <(echo_rc)
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment