Skip to content
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

fixing linux headers in buildlibs.sh #107

Closed
wants to merge 23 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

Con4m makes it easy to give users rich configurability via config
files and command line flags. You just have to write a spec to get
your config file format and your command line flags / parsing.
your config file format and your command line flags / parsing.

You can do all of your input validation either through Con4m's built
in constraints, or through custom validation routines (themselves
Expand Down Expand Up @@ -231,4 +231,3 @@ Con4m is open source under the Apache 2.0 license.
Con4m was written by John Viega ([email protected]), originally
for Chalk and other to-be-named projects, because other options for
flexibile configs (like HCL, or YAML DSLs) all kinda suck.

6 changes: 2 additions & 4 deletions con4m.nimble
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# Package
version = "0.1.0"
version = "0.0.2"
author = "John Viega"
description = "A generic configuration file format that allows for flexible, lightweight scripting."
license = "Apache-2.0"
bin = @["con4m"]
srcDir = "files"
installExt = @["nim", "c4m", "c42spec", "c", "h", "a"]
installExt = @["nim", "c4m", "c42spec", "c", "h", "a", "sh", "specs"]

# Dependencies
requires "nim >= 1.6.12"
Expand All @@ -20,5 +20,3 @@ task ctest, "Build libcon4m":
exec "cc -Wall -o bin/test src/c/test.c lib/libcon4m.a -I ~/.choosenim/toolchains/nim-1.6.10/lib/ -lc -lm -ldl"
else:
echo "Platform ", hostOs, " Not supported."

discard staticExec("pwd > /tmp/con4m-build-dir")
94 changes: 39 additions & 55 deletions config.nims
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,15 @@ when not defined(debug):
switch("d", "release")
switch("opt", "speed")

var targetArch = hostCPU

when defined(macosx):
# -d:arch=amd64 will allow you to specifically cross-compile to intel.
# The .strdefine. pragma sets the variable from the -d: flag w/ the same
# name, overriding the value of the const.
const arch {.strdefine.} = "detect"

# This one is used if we can't find where con4m ends up after nimble
# grabs the dep.
const nimblePkgRoot {.strdefine.} = "~/.nimble/pkgs2/"

var
targetArch = arch
targetStr = ""

if arch == "detect":
Expand All @@ -47,70 +44,57 @@ when defined(macosx):
echo "Override: arch = " & arch

if targetArch == "arm64":
echo "Building for arm64"
targetStr = "arm64-apple-macos11"
elif targetArch == "amd64":
targetStr = "x86_64-apple-macos11"
echo "Building for amd64"
else:
echo "Invalid target architecture for MacOs: " & arch
quit(1)

switch("cpu", targetArch)
switch("passc", "-flto -target " & targetStr)
switch("passl", "-flto -w -target " & targetStr &
"-Wl,-object_path_lto,lto.o")

var deploc: string
elif defined(linux):
switch("passc", "-static")
switch("passl", "-static")
else:
echo "Platform not supported."
quit(1)

# If we are developing nim, everything has to be under files,
# but if it's building the exe from nimble, there will be no files dir.
if "con4m" notin getCurrentDir():
var
subdir = ""

if nimblePkgRoot.startsWith("~"):
deploc = getenv("HOME")
if not deploc.endswith("/"):
deploc &= "/"
for item in listDirs(thisDir()):
if item.endswith("/files"):
subdir = "/files"
break

let rest = nimblePkgRoot[1 .. ^1]
if not rest.startswith("/"):
deploc &= rest
else:
deploc &= rest[1 .. ^1]
proc getEnvDir(s: string, default = ""): string =
result = getEnv(s, default)
if not result.endsWith("/"):
result &= "/"

else:
deploc = nimblePkgRoot

if not dirExists(deploc):
echo "This is a hacky work-around for a nimble issue."
exec "nimble install con4m"
if not dirExists(deploc):
echo "Cannot find nimble path. Please set -d:nimblePkgRoot to the ",
"location where con4m lives (usually ~/.nimble/pkgs2/)"
quit(1)

let
latest = staticExec("ls " & deploc & " | egrep \"^con4m\" | " &
"sort -V | tail -1")
if latest.strip() == "":
echo "******************** WARNING ********************\n",
"Cannot find con4m install. You are probably installing ",
"it via Chalk or some similar program that uses con4m, and do ",
"not yet have con4m installed. The newer Nim package manager ",
"won't easily tell us where the tmp files it downloaded live, ",
"until it moves them in place, once the con4m install is done.\n",
"As a result, con4m will build without libraries it need to run.\n",
"When it's done, please run:\n\n",
" rm `which con4m`\n\n",
"and rebuild the app you are trying to build!"

deploc = deploc & "/" & latest & "/deps/macos/"
else:
deploc = getCurrentDir() & "/files/deps/macos/"
exec thisDir() & subdir & "/bin/buildlibs.sh " & thisDir() & "/files/deps"

var
default = getEnvDir("HOME") & ".local/c0"
localDir = getEnvDir("LOCAL_INSTALL_DIR", default)
libDir = localdir & "libs"
libs = ["pcre", "ssl", "crypto"]

when defined(linux):
var
muslPath = localdir & "musl/bin/musl-gcc"

switch("gcc.exe", muslPath)
switch("gcc.linkerexe", muslPath)

if dirExists(depLoc):
let
libs = ["ssl", "crypto"]
libDir = deploc & targetArch & "/"
for item in libs:
let libFile = "lib" & item & ".a"

for item in libs:
let libFile = "lib" & item & ".a"
switch("passL", libDir & libFile)
switch("dynlibOverride", item)
switch("passL", libDir & "/" & libFile)
switch("dynlibOverride", item)
220 changes: 220 additions & 0 deletions files/bin/buildlibs.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,220 @@
#!/bin/bash

# ensure there is no silent failures
set -eEu
set -o pipefail

ARCH=$(uname -m)
OS=$(uname -o)

if [[ ${ARCH} = "x86_64" ]] ; then
NIMARCH=amd64
else
NIMARCH=arm64
fi

if [[ ${OS} = "Darwin" ]] ; then
# Not awesome, but this is what nim calls it.
OS=macosx
else
# We don't support anything else at the moment.
OS=linux
fi

DEPS_DIR=${DEPS_DIR:-${HOME}/.local/c0}

PKG_LIBS=${1}/lib/${OS}-${NIMARCH}
MY_LIBS=${DEPS_DIR}/libs
SRC_DIR=${DEPS_DIR}/src
MUSL_DIR=${DEPS_DIR}/musl
MUSL_GCC=${MUSL_DIR}/bin/musl-gcc

mkdir -p ${MY_LIBS}
cd ${DEPS_DIR}

# The paste doesn't work from stdin on MacOS, so leave this as is, please.
export OPENSSL_CONFIG_OPTS=$(echo "
enable-ec_nistp_64_gcc_128
no-afalgeng
no-apps
no-bf
no-camellia
no-cast
no-comp
no-deprecated
no-des
no-docs
no-dtls
no-dtls1
no-egd
no-engine
no-err
no-idea
no-md2
no-md4
no-mdc2
no-psk
no-quic
no-rc2
no-rc4
no-rc5
no-seed
no-shared
no-srp
no-ssl
no-tests
no-tls1
no-tls1_1
no-uplink
no-weak-ssl-ciphers
no-zlib
" | tr '\n' ' ')

function color {
case $1 in
black) CODE=0 ;;
red) CODE=1 ;; RED) CODE=9 ;;
green) CODE=2 ;; GREEN) CODE=10 ;;
yellow) CODE=3 ;; YELLOW) CODE=11 ;;
blue) CODE=4 ;; BLUE) CODE=12 ;;
magenta) CODE=5 ;; MAGENTA) CODE=13 ;;
cyan) CODE=6 ;; CYAN) CODE=14 ;;
white) CODE=7 ;; WHITE) CODE=15 ;;
grey) CODE=8 ;; *) CODE=$1 ;;
esac
shift

echo -n $(tput setaf ${CODE})$@$(tput op)
}

function colorln {
echo $(color $@)
}

function copy_from_package {
for item in ${@}
do
if [[ ! -f ${MY_LIBS}/${item} ]] ; then
if [[ ! -f ${PKG_LIBS}/${item} ]] ; then
return 1
else
cp ${PKG_LIBS}/${item} ${MY_LIBS}
fi
fi
done
return 0
}

function get_src {
mkdir -p ${SRC_DIR}
cd ${SRC_DIR}

if [[ ! -d ${SRC_DIR}/${1} ]] ; then
echo $(color CYAN Downloading ${1} from:) ${2}
git clone ${2}
fi
if [[ ! -d ${1} ]] ; then
echo $(color RED Could not create directory: ) ${SRC_DIR}/${1}
exit 1
fi
cd ${1}
}

function ensure_musl {
if [[ ${OS} = "macosx" ]] ; then
return
fi
if [[ ! -f ${MUSL_GCC} ]] ; then
get_src musl git://git.musl-libc.org/musl
colorln CYAN Building musl
unset CC
./configure --disable-shared --prefix=${MUSL_DIR}
make clean
make
make install
mv lib/*.a ${MY_LIBS}

if [[ -f ${MUSL_GCC} ]] ; then
echo $(color GREEN Installed musl wrapper to:) ${MUSL_GCC}
else
colorln RED Installation of musl failed!
exit 1
fi
fi
export CC=${MUSL_GCC}
export CXX=${MUSL_GCC}
}

function install_kernel_headers {
if [[ ${OS} = "macosx" ]] ; then
return
fi
colorln CYAN Installing kernel headers needed for musl install
get_src kernel-headers https://github.com/sabotage-linux/kernel-headers.git
make ARCH=${ARCH} prefix= DESTDIR=${MUSL_DIR} install
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this has the actual fix by removing prefix and updating DESTDIR

}

function ensure_openssl {

if ! copy_from_package libssl.a libcrypto.a ; then
ensure_musl
install_kernel_headers

get_src openssl https://github.com/openssl/openssl.git
colorln CYAN Building openssl
if [[ ${OS} == "macosx" ]]; then
./config ${OPENSSL_CONFIG_OPTS}
else
./config ${OPENSSL_CONFIG_OPTS} -static
fi
make clean
make build_libs
mv *.a ${MY_LIBS}
if [[ -f ${MY_LIBS}/libssl.a ]] && [[ -f ${MY_LIBS}/libcrypto.a ]] ; then
echo $(color GREEN Installed openssl libs to:) ${MY_LIBS}
else
colorln RED Installation of openssl failed!
exit 1
fi
fi
}

function ensure_pcre {
if ! copy_from_package libpcre.a ; then

get_src pcre https://github.com/luvit/pcre.git
colorln CYAN "Building libpcre"
# For some reason, build fails on arm if we try to compile w/ musl?
unset CC
./configure --disable-cpp --disable-shared
make clean
make

mv .libs/libpcre.a ${MY_LIBS}
if [[ -f ${MY_LIBS}/libpcre.a ]] ; then
echo $(color GREEN Installed libpcre to:) ${MY_LIBS}/libpcrea.
else
colorln RED "Installation of libprce failed. This may be due to missing build dependencies. Please make sure autoconf, m4 and perl are installed."
exit 1
fi
fi
}

function remove_src {
# Don't nuke the src if CON4M_DEV is on.
if [[ -d ${SRC_DIR} ]] ; then
if [[ -z ${CON4M_DEV+woo} ]] ; then
colorln CYAN Removing code \(because CON4M_DEV is not set\)
rm -rf ${SRC_DIR}
else
colorln CYAN Keeping source code \(CON4M_DEV is set\)
fi
fi
}

ensure_musl
ensure_openssl
ensure_pcre

colorln GREEN All dependencies satisfied.
remove_src
Binary file added files/deps/lib/macosx-amd64/libcrypto.a
Binary file not shown.
Binary file added files/deps/lib/macosx-amd64/libpcre.a
Binary file not shown.
Binary file added files/deps/lib/macosx-amd64/libssl.a
Binary file not shown.
Binary file added files/deps/lib/macosx-arm64/libcrypto.a
Binary file not shown.
Binary file added files/deps/lib/macosx-arm64/libpcre.a
Binary file not shown.
Binary file added files/deps/lib/macosx-arm64/libssl.a
Binary file not shown.