From 233b1d886030585b58f65aacf529297892c7830c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20P=C3=B6chtrager?= Date: Thu, 22 Sep 2016 18:58:45 +0200 Subject: [PATCH] Various Cygwin fixes (#87, #88) --- README.CYGWIN.md | 7 +-- build.sh | 3 ++ patches/cctools-ld64-cygwin-buildfix.patch | 60 ++++++++++++++++++++++ tools/gen_cyglto_dll.sh | 51 ++++++++++++++++++ wrapper/tools.cpp | 3 +- wrapper/wrapper.workspace | 2 +- 6 files changed, 121 insertions(+), 5 deletions(-) create mode 100644 patches/cctools-ld64-cygwin-buildfix.patch create mode 100755 tools/gen_cyglto_dll.sh diff --git a/README.CYGWIN.md b/README.CYGWIN.md index 1a0a09c..3e185fe 100644 --- a/README.CYGWIN.md +++ b/README.CYGWIN.md @@ -16,11 +16,12 @@ Click on setup-*.exe and install the following packages: For LTO support you will also need: -`libllvm-devel`, `libllvm` and `gendef` +`libllvm-devel`, `libllvm`, `libedit` and `libffi` -The cygLTO.dll import library is missing, create it by hand: +The Cygwin package of libLLVM lacks the LTO library, but you can create that dll +by issuing the following command: -`gendef /bin/cygLTO.dll && dlltool -d cygLTO.def -l /lib/libLTO.dll.a && rm cygLTO.def` +`./tools/gen_cyglto_dll.sh` #### Step 2: #### diff --git a/build.sh b/build.sh index 361f6a8..23f7155 100755 --- a/build.sh +++ b/build.sh @@ -150,6 +150,9 @@ popd &>/dev/null patch -p0 < $PATCH_DIR/cctools-ld64-1.patch patch -p0 < $PATCH_DIR/cctools-ld64-2.patch patch -p1 < $PATCH_DIR/cctools-ld64-misc-fixes.patch +pushd .. &>/dev/null +patch -p0 < $PATCH_DIR/cctools-ld64-cygwin-buildfix.patch +popd &>/dev/null echo "" CONFFLAGS="--prefix=$TARGET_DIR --target=x86_64-apple-$TARGET " CONFFLAGS+="--disable-clang-as " diff --git a/patches/cctools-ld64-cygwin-buildfix.patch b/patches/cctools-ld64-cygwin-buildfix.patch new file mode 100644 index 0000000..0a24aac --- /dev/null +++ b/patches/cctools-ld64-cygwin-buildfix.patch @@ -0,0 +1,60 @@ +diff --git cctools/configure.ac cctools/configure.ac +index 56e8f24..5219e3d 100644 +--- cctools/configure.ac ++++ cctools/configure.ac +@@ -49,7 +49,8 @@ case $host_os in + ;; + cygwin* ) + iscygwin=yes +- EXTRAFLAGS="-D__LARGE64_FILES -fno-PIC" ++ # https://github.com/tpoechtrager/osxcross/issues/87 ++ EXTRAFLAGS="-D__LARGE64_FILES -D_GNU_SOURCE -fno-PIC" + WARNINGS="-Wno-attributes" + XCC="gcc" + XCXX="g++" +diff --git cctools/include/foreign/machine/endian.h cctools/include/foreign/machine/endian.h +index 6cf3901..0920fd6 100644 +--- cctools/include/foreign/machine/endian.h ++++ cctools/include/foreign/machine/endian.h +@@ -28,7 +28,8 @@ + /* + * Copyright 1995 NeXT Computer, Inc. All rights reserved. + */ +-#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) ++#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || \ ++ defined(__DragonFly__) || defined(__CYGWIN__) + #include_next + #else + #ifndef _BSD_MACHINE_ENDIAN_H_ +@@ -45,4 +46,5 @@ + #endif + + #endif /* _BSD_MACHINE_ENDIAN_H_ */ +-#endif /* __FreeBSD__ || __NetBSD__ || __OpenBSD__ || __DragonFly__ */ ++#endif /* __FreeBSD__ || __NetBSD__ || __OpenBSD__ || ++ __DragonFly__ || __CYGWIN__ */ +diff --git usage_examples/ios_toolchain/wrapper.c usage_examples/ios_toolchain/wrapper.c +index c102d79..b88d35a 100644 +--- usage_examples/ios_toolchain/wrapper.c ++++ usage_examples/ios_toolchain/wrapper.c +@@ -86,6 +86,7 @@ char *get_executable_path(char *epath, size_t buflen) + l = strlen(epath); + #else + ssize_t l = readlink("/proc/self/exe", epath, buflen); ++ if (l > 0) epath[l] = '\0'; + #endif + if (l <= 0) return NULL; + epath[buflen - 1] = '\0'; +--- cctools/configure ++++ cctools/configure +@@ -2627,7 +2627,8 @@ + ;; + cygwin* ) + iscygwin=yes +- EXTRAFLAGS="-D__LARGE64_FILES -fno-PIC" ++ # https://github.com/tpoechtrager/osxcross/issues/87 ++ EXTRAFLAGS="-D__LARGE64_FILES -D_GNU_SOURCE -fno-PIC" + WARNINGS="-Wno-attributes" + XCC="gcc" + XCXX="g++" + diff --git a/tools/gen_cyglto_dll.sh b/tools/gen_cyglto_dll.sh new file mode 100755 index 0000000..0c15c84 --- /dev/null +++ b/tools/gen_cyglto_dll.sh @@ -0,0 +1,51 @@ +#!/usr/bin/env bash + +pushd "${0%/*}/.." &>/dev/null +source tools/tools.sh + +if [[ $PLATFORM != CYGWIN* ]]; then + exit 1 +fi + +LLVM_CONFIG="llvm-config" + +CXXFLAGS="$($LLVM_CONFIG --cxxflags) -fno-PIC" +LDFLAGS="$($LLVM_CONFIG --cxxflags) -Wl,-s" +INCDIR=$($LLVM_CONFIG --includedir) +LIBDIR=$($LLVM_CONFIG --libdir) +LIBS=$($LLVM_CONFIG --libs all) +SYSLIBS="$($LLVM_CONFIG --system-libs) -ledit -lffi" + +VERSION=$($LLVM_CONFIG --version | awk -F \. {'print $1$2'} | sed 's/svn//g') + +set -e +TMP=$(mktemp -d) +set +e + +pushd $TMP &>/dev/null +wget https://raw.githubusercontent.com/llvm-mirror/llvm/release_$VERSION/tools/lto/lto.cpp +wget https://raw.githubusercontent.com/llvm-mirror/llvm/release_$VERSION/tools/lto/LTODisassembler.cpp +wget https://raw.githubusercontent.com/llvm-mirror/llvm/release_$VERSION/tools/lto/lto.exports + +echo "{" > cyglto.exports +echo " global:" >> cyglto.exports +while read p; do + echo " $p;" >> cyglto.exports +done < lto.exports +echo " LLVM*;" >> cyglto.exports +echo " local: *;" >> cyglto.exports +echo "};" >> cyglto.exports + +popd &>/dev/null + +set -x + +g++ -shared \ + -L$LIBDIR -I$INCDIR $CXXFLAGS $LDFLAGS \ + -Wl,--whole-archive $LIBS -Wl,--no-whole-archive $SYSLIBS \ + $TMP/lto.cpp $TMP/LTODisassembler.cpp -Wl,-version-script,$TMP/cyglto.exports \ + -o /bin/cygLTO.dll -Wl,--out-implib,/lib/libLTO.dll.a + +rm -rf $TMP + +popd &>/dev/null diff --git a/wrapper/tools.cpp b/wrapper/tools.cpp index 54fb5ae..6b839a6 100644 --- a/wrapper/tools.cpp +++ b/wrapper/tools.cpp @@ -135,8 +135,9 @@ char *getExecutablePath(char *buf, size_t len) { l = 0; delete[] argv; #else - ssize_t l = readlink("/proc/self/exe", buf, len); + ssize_t l = readlink("/proc/self/exe", buf, len - 1); assert(l > 0 && "/proc not mounted?"); + if (l > 0) buf[l] = '\0'; #endif if (l <= 0) return nullptr; diff --git a/wrapper/wrapper.workspace b/wrapper/wrapper.workspace index 782cb19..e32a680 100644 --- a/wrapper/wrapper.workspace +++ b/wrapper/wrapper.workspace @@ -1,5 +1,5 @@ - +