Commit 6ae94c20 authored by Hugo Beauzée-Luyssen's avatar Hugo Beauzée-Luyssen

vlc-debian-llvm-mingw: Update from upstream

parent 67e3907e
......@@ -2,7 +2,7 @@
#
FROM registry.videolan.org:5000/videolan-base-sid:20180419103824
ENV IMAGE_DATE=20181108230
ENV IMAGE_DATE=201811122123
RUN apt-get update -qq && apt-get install -qqy \
git wget bzip2 file unzip libtool pkg-config cmake build-essential \
......@@ -35,7 +35,7 @@ ENV TOOLCHAIN_PREFIX=/opt/llvm-mingw
COPY build-llvm.sh strip-llvm.sh ./
RUN ./build-llvm.sh $TOOLCHAIN_PREFIX && \
./strip-llvm.sh $TOOLCHAIN_PREFIX && \
rm -rf /build
rm -rf /build/*
ARG TOOLCHAIN_ARCHS="i686 x86_64 armv7 aarch64"
......@@ -43,29 +43,29 @@ ARG TOOLCHAIN_ARCHS="i686 x86_64 armv7 aarch64"
COPY wrappers/*.sh wrappers/*.c ./wrappers/
COPY install-wrappers.sh .
RUN ./install-wrappers.sh $TOOLCHAIN_PREFIX && \
rm -rf /build
rm -rf /build/*
# Build MinGW-w64, compiler-rt and mingw-w64's extra libraries
COPY build-mingw-w64.sh build-mingw-w64-widl.sh build-compiler-rt.sh build-mingw-w64-libraries.sh ./
COPY build-mingw-w64.sh build-compiler-rt.sh build-mingw-w64-libraries.sh ./
RUN ./build-mingw-w64.sh $TOOLCHAIN_PREFIX && \
./build-compiler-rt.sh $TOOLCHAIN_PREFIX && \
./build-mingw-w64-libraries.sh $TOOLCHAIN_PREFIX && \
rm -rf /build
rm -rf /build/*
# Build libunwind/libcxxabi/libcxx
COPY build-libcxx.sh merge-archives.sh ./
RUN ./build-libcxx.sh $TOOLCHAIN_PREFIX && \
rm -rf /build
rm -rf /build/*
# Build sanitizers
COPY build-compiler-rt.sh .
RUN ./build-compiler-rt.sh $TOOLCHAIN_PREFIX --build-sanitizers && \
rm -rf /build
rm -rf /build/*
# Build libssp
COPY build-libssp.sh libssp-Makefile ./
RUN ./build-libssp.sh $TOOLCHAIN_PREFIX && \
rm -rf /build
rm -rf /build/*
# Cheating: Pull strip and objcopy from the normal binutils package.
RUN apt-get update -qq && \
......
......@@ -21,6 +21,8 @@ if [ -z "$PREFIX" ]; then
fi
export PATH=$PREFIX/bin:$PATH
: ${CORES:=$(nproc 2>/dev/null)}
: ${CORES:=$(sysctl -n hw.ncpu 2>/dev/null)}
: ${CORES:=4}
: ${ARCHS:=${TOOLCHAIN_ARCHS-i686 x86_64 armv7 aarch64}}
......@@ -40,7 +42,7 @@ cd compiler-rt
if [ -n "$SYNC" ] || [ -n "$CHECKOUT" ]; then
[ -z "$SYNC" ] || git fetch
git checkout fedf618bc7021e1038855668cf1740966b8a414d
git checkout 0492c25c247e31fe23570fe0cf6e9801301ab069
fi
for arch in $ARCHS; do
......
......@@ -25,6 +25,8 @@ if [ -z "$PREFIX" ]; then
fi
export PATH=$PREFIX/bin:$PATH
: ${CORES:=$(nproc 2>/dev/null)}
: ${CORES:=$(sysctl -n hw.ncpu 2>/dev/null)}
: ${CORES:=4}
: ${ARCHS:=${TOOLCHAIN_ARCHS-i686 x86_64 armv7 aarch64}}
......
......@@ -9,6 +9,8 @@ fi
PREFIX="$1"
export PATH=$PREFIX/bin:$PATH
: ${CORES:=$(nproc 2>/dev/null)}
: ${CORES:=$(sysctl -n hw.ncpu 2>/dev/null)}
: ${CORES:=4}
: ${ARCHS:=${TOOLCHAIN_ARCHS-i686 x86_64 armv7 aarch64}}
......
......@@ -22,6 +22,8 @@ if [ -z "$PREFIX" ]; then
exit 1
fi
: ${CORES:=$(nproc 2>/dev/null)}
: ${CORES:=$(sysctl -n hw.ncpu 2>/dev/null)}
: ${CORES:=4}
if [ ! -d llvm ]; then
......@@ -51,13 +53,13 @@ fi
if [ -n "$SYNC" ] || [ -n "$CHECKOUT" ]; then
cd llvm
[ -z "$SYNC" ] || git fetch
git checkout a978525b6415baf1f6a3f49b2cc8b50167f06e60
git checkout c76c02e1ed1a158628019f61aea45fcf87712d2c
cd tools/clang
[ -z "$SYNC" ] || git fetch
git checkout 122fb01a0d3afdb020866c719896161a1b829e03
git checkout e3de7bb263271008c55cc8bf3186a3a08999fbaa
cd ../lld
[ -z "$SYNC" ] || git fetch
git checkout d0c1d02fc3bb851b22f4e5396c9c5afa91842c7b
git checkout 935d766de8b251de001fed5f44b9567ccf6cadb1
cd ../../..
fi
......@@ -67,20 +69,35 @@ if [ -n "$(which ninja)" ]; then
fi
if [ -n "$HOST" ]; then
find_native_tools() {
if [ -d llvm/build/bin ]; then
echo $(pwd)/llvm/build/bin
elif [ -d llvm/build-asserts/bin ]; then
echo $(pwd)/llvm/build-asserts/bin
elif [ -d llvm/build-noasserts/bin ]; then
echo $(pwd)/llvm/build-noasserts/bin
elif [ -n "$(which llvm-tblgen)" ]; then
echo $(dirname $(which llvm-tblgen))
fi
}
native=$(find_native_tools)
if [ -z "$native" ]; then
# As we don't do any install here, the target prefix shouldn't actually
# be created.
HOST="" BUILDTARGETS="llvm-tblgen clang-tblgen llvm-config" $0 $PREFIX/llvmtools
native=$(find_native_tools)
if [ -z "$native" ]; then
echo Unable to find the newly built llvm-tblgen
exit 1
fi
fi
CMAKEFLAGS="$CMAKEFLAGS -DCMAKE_SYSTEM_NAME=Windows"
CMAKEFLAGS="$CMAKEFLAGS -DCMAKE_CROSSCOMPILE=1"
CMAKEFLAGS="$CMAKEFLAGS -DCMAKE_C_COMPILER=$HOST-gcc"
CMAKEFLAGS="$CMAKEFLAGS -DCMAKE_CXX_COMPILER=$HOST-g++"
CMAKEFLAGS="$CMAKEFLAGS -DCMAKE_RC_COMPILER=$HOST-windres"
if [ -d llvm/build/bin ]; then
native=$(pwd)/llvm/build/bin
elif [ -d llvm/build-asserts/bin ]; then
native=$(pwd)/llvm/build-asserts/bin
elif [ -d llvm/build-noasserts/bin ]; then
native=$(pwd)/llvm/build-noasserts/bin
else
native=$(dirname $(which llvm-tblgen))
fi
CMAKEFLAGS="$CMAKEFLAGS -DLLVM_TABLEGEN=$native/llvm-tblgen"
CMAKEFLAGS="$CMAKEFLAGS -DCLANG_TABLEGEN=$native/clang-tblgen"
CMAKEFLAGS="$CMAKEFLAGS -DLLVM_CONFIG_PATH=$native/llvm-config"
......@@ -112,8 +129,10 @@ cmake \
-DLLVM_TOOLCHAIN_TOOLS="llvm-ar;llvm-ranlib;llvm-objdump;llvm-rc;llvm-cvtres;llvm-nm;llvm-strings;llvm-readobj;llvm-dlltool;llvm-pdbutil" \
$CMAKEFLAGS \
..
: ${BUILDTARGETS:=install/strip}
if [ -n "$NINJA" ]; then
ninja install/strip
ninja $BUILDTARGETS
else
make -j$CORES install/strip
make -j$CORES $BUILDTARGETS
fi
......@@ -9,6 +9,8 @@ fi
PREFIX="$1"
export PATH=$PREFIX/bin:$PATH
: ${CORES:=$(nproc 2>/dev/null)}
: ${CORES:=$(sysctl -n hw.ncpu 2>/dev/null)}
: ${CORES:=4}
: ${ARCHS:=${TOOLCHAIN_ARCHS-i686 x86_64 armv7 aarch64}}
......
#!/bin/sh
set -e
if [ $# -lt 1 ]; then
echo $0 dest
exit 1
fi
PREFIX="$1"
# Not adding $PREFIX/bin to $PATH here, since that is expected to be
# a cross toolchain; the compiler is expected to be in $PATH already here.
: ${CORES:=4}
: ${ARCHS:=${TOOLCHAIN_ARCHS-i686 x86_64 armv7 aarch64}}
if [ -n "$HOST" ]; then
CONFIGFLAGS="--host=$HOST"
CROSS_NAME=$HOST-
fi
cd mingw-w64/mingw-w64-tools/widl
for arch in $ARCHS; do
mkdir -p build-$CROSS_NAME$arch
cd build-$CROSS_NAME$arch
../configure --prefix=$PREFIX --target=$arch-w64-mingw32 $CONFIGFLAGS LDFLAGS="-Wl,-s"
make -j$CORES
make install
cd ..
done
......@@ -2,13 +2,29 @@
set -e
if [ $# -lt 1 ]; then
echo $0 dest
while [ $# -gt 0 ]; do
if [ "$1" = "--skip-include-triplet-prefix" ]; then
SKIP_INCLUDE_TRIPLET_PREFIX=1
else
PREFIX="$1"
fi
shift
done
if [ -z "$PREFIX" ]; then
echo $0 [--skip-include-triplet-prefix] dest
exit 1
fi
PREFIX="$1"
export PATH=$PREFIX/bin:$PATH
if [ -z "$HOST" ]; then
# The newly built toolchain isn't crosscompiled; add it to the path.
export PATH=$PREFIX/bin:$PATH
else
# Crosscompiling the toolchain itself; the cross compiler is
# expected to already be in $PATH.
true
fi
: ${CORES:=$(nproc 2>/dev/null)}
: ${CORES:=$(sysctl -n hw.ncpu 2>/dev/null)}
: ${CORES:=4}
: ${ARCHS:=${TOOLCHAIN_ARCHS-i686 x86_64 armv7 aarch64}}
......@@ -21,47 +37,87 @@ cd mingw-w64
if [ -n "$SYNC" ] || [ -n "$CHECKOUT" ]; then
[ -z "$SYNC" ] || git fetch
git checkout 1b29d1bc58910a4c21ff2c5c804bf06821032348
git checkout c69c7a706d767c5ca3c7d1c70887fcd8e1f940b3
fi
cd mingw-w64-headers
mkdir -p build
cd build
../configure --prefix=$PREFIX/generic-w64-mingw32 \
--enable-secure-api --enable-idl --with-default-win32-winnt=0x600 --with-default-msvcrt=ucrt INSTALL="install -C"
make install
cd ../..
for arch in $ARCHS; do
mkdir -p $PREFIX/$arch-w64-mingw32
ln -sfn ../generic-w64-mingw32/include $PREFIX/$arch-w64-mingw32/include
done
# If crosscompiling the toolchain itself, we already have a mingw-w64
# runtime and don't need to rebuild it.
if [ -z "$HOST" ]; then
if [ -z "$SKIP_INCLUDE_TRIPLET_PREFIX" ]; then
HEADER_ROOT=$PREFIX/generic-w64-mingw32
else
HEADER_ROOT=$PREFIX
fi
cd mingw-w64-headers
mkdir -p build
cd build
../configure --prefix=$HEADER_ROOT \
--enable-secure-api --enable-idl --with-default-win32-winnt=0x600 --with-default-msvcrt=ucrt INSTALL="install -C"
make install
cd ../..
if [ -z "$SKIP_INCLUDE_TRIPLET_PREFIX" ]; then
for arch in $ARCHS; do
mkdir -p $PREFIX/$arch-w64-mingw32
ln -sfn ../generic-w64-mingw32/include $PREFIX/$arch-w64-mingw32/include
done
fi
cd mingw-w64-crt
for arch in $ARCHS; do
mkdir -p build-$arch
cd build-$arch
case $arch in
armv7)
FLAGS="--disable-lib32 --disable-lib64 --enable-libarm32"
;;
aarch64)
FLAGS="--disable-lib32 --disable-lib64 --enable-libarm64"
;;
i686)
FLAGS="--enable-lib32 --disable-lib64"
;;
x86_64)
FLAGS="--disable-lib32 --enable-lib64"
;;
esac
FLAGS="$FLAGS --with-default-msvcrt=ucrt"
../configure --host=$arch-w64-mingw32 --prefix=$PREFIX/$arch-w64-mingw32 $FLAGS \
CC=$arch-w64-mingw32-clang AR=llvm-ar RANLIB=llvm-ranlib DLLTOOL=llvm-dlltool
make -j$CORES
make install
cd ..
done
cd ..
fi
cd mingw-w64-crt
if [ -n "$HOST" ]; then
CONFIGFLAGS="$CONFIGFLAGS --host=$HOST"
CROSS_NAME=$HOST-
EXEEXT=.exe
fi
if [ -n "$SKIP_INCLUDE_TRIPLET_PREFIX" ]; then
CONFIGFLAGS="$CONFIGFLAGS --with-widl-includedir=$PREFIX/include"
# If using the same includedir for all archs, it's enough to
# build one single binary.
ALL_ARCHS="$ARCHS"
ARCHS=x86_64
fi
cd mingw-w64-tools/widl
for arch in $ARCHS; do
mkdir -p build-$arch
cd build-$arch
case $arch in
armv7)
FLAGS="--disable-lib32 --disable-lib64 --enable-libarm32"
;;
aarch64)
FLAGS="--disable-lib32 --disable-lib64 --enable-libarm64"
;;
i686)
FLAGS="--enable-lib32 --disable-lib64"
;;
x86_64)
FLAGS="--disable-lib32 --enable-lib64"
;;
esac
FLAGS="$FLAGS --with-default-msvcrt=ucrt"
../configure --host=$arch-w64-mingw32 --prefix=$PREFIX/$arch-w64-mingw32 $FLAGS \
CC=$arch-w64-mingw32-clang AR=llvm-ar RANLIB=llvm-ranlib DLLTOOL=llvm-dlltool
mkdir -p build-$CROSS_NAME$arch
cd build-$CROSS_NAME$arch
../configure --prefix=$PREFIX --target=$arch-w64-mingw32 $CONFIGFLAGS LDFLAGS="-Wl,-s"
make -j$CORES
make install
cd ..
done
cd ..
cd ..
./build-mingw-w64-widl.sh "$PREFIX"
if [ -n "$SKIP_INCLUDE_TRIPLET_PREFIX" ]; then
cd $PREFIX/bin
for arch in $ALL_ARCHS; do
if [ "$arch" != "$ARCHS" ]; then
ln -sf $ARCHS-w64-mingw32-widl$EXEEXT $arch-w64-mingw32-widl$EXEEXT
fi
done
fi
......@@ -11,25 +11,69 @@ PREFIX="$1"
: ${ARCHS:=${TOOLCHAIN_ARCHS-i686 x86_64 armv7 aarch64}}
if [ -n "$HOST" ] && [ -z "$CC" ]; then
CC=${HOST}-gcc
CC=$HOST-gcc
fi
: ${CC:=cc}
case $(uname) in
MINGW*)
EXEEXT=.exe
;;
esac
if [ -n "$EXEEXT" ]; then
CLANG_MAJOR=$(basename $(echo $PREFIX/lib/clang/* | awk '{print $NF}') | cut -f 1 -d .)
WRAPPER_FLAGS="$WRAPPER_FLAGS -municode -DCLANG=\"clang-$CLANG_MAJOR\""
fi
mkdir -p $PREFIX/bin
cp wrappers/*-wrapper.sh $PREFIX/bin
if [ -n "$HOST" ]; then
# TODO: If building natively on msys, pick up the default HOST value from there.
WRAPPER_FLAGS="$WRAPPER_FLAGS -DDEFAULT_TARGET=\"$HOST\""
for i in wrappers/*-wrapper.sh; do
cat $i | sed 's/^DEFAULT_TARGET=.*/DEFAULT_TARGET='$HOST/ > $PREFIX/bin/$(basename $i)
done
fi
$CC wrappers/change-pe-arch.c -o $PREFIX/bin/change-pe-arch$EXEEXT
$CC wrappers/clang-target-wrapper.c -o $PREFIX/bin/clang-target-wrapper$EXEEXT -O2 -Wl,-s $WRAPPER_FLAGS
$CC wrappers/windres-wrapper.c -o $PREFIX/bin/windres-wrapper$EXEEXT -O2 -Wl,-s $WRAPPER_FLAGS
if [ -n "$EXEEXT" ]; then
# For Windows, we should prefer the executable wrapper, which also works
# when invoked from outside of MSYS.
CTW_SUFFIX=$EXEEXT
CTW_LINK_SUFFIX=$EXEEXT
else
CTW_SUFFIX=.sh
fi
cd $PREFIX/bin
for arch in $ARCHS; do
for exec in clang clang++ gcc g++; do
ln -sf clang-target-wrapper.sh $arch-w64-mingw32-$exec
for exec in clang clang++ gcc g++ cc c99 c11 c++; do
ln -sf clang-target-wrapper$CTW_SUFFIX $arch-w64-mingw32-$exec$CTW_LINK_SUFFIX
done
for exec in ar ranlib nm strings; do
ln -sf llvm-$exec$EXEEXT $arch-w64-mingw32-$exec || true
ln -sf llvm-$exec$EXEEXT $arch-w64-mingw32-$exec$EXEEXT || true
done
for exec in ld objdump windres dlltool; do
for exec in windres; do
ln -sf $exec-wrapper$EXEEXT $arch-w64-mingw32-$exec$EXEEXT
done
for exec in ld objdump dlltool; do
ln -sf $exec-wrapper.sh $arch-w64-mingw32-$exec
done
for exec in objcopy strip; do
ln -sf objcopy-wrapper.sh $arch-w64-mingw32-$exec
done
done
if [ -n "$EXEEXT" ]; then
if [ ! -L clang$EXEEXT ]; then
mv clang$EXEEXT clang-$CLANG_MAJOR$EXEEXT
fi
if [ -n "$HOST" ]; then
for exec in clang clang++ gcc g++ cc c99 c11 c++ ar ranlib nm strings widl windres; do
ln -sf $HOST-$exec$EXEEXT $exec$EXEEXT
done
for exec in ld objdump dlltool objcopy strip; do
ln -sf $HOST-$exec $exec
done
fi
fi
/*
* Copyright (c) 2018 Martin Storsjo
*
* This file is part of llvm-mingw.
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#ifdef UNICODE
#define _UNICODE
#endif
#ifndef CLANG
#define CLANG "clang"
#endif
#ifndef DEFAULT_TARGET
#define DEFAULT_TARGET "x86_64-w64-mingw32"
#endif
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#include <tchar.h>
#include <windows.h>
#include <process.h>
#define EXECVP_CAST
#else
#include <unistd.h>
typedef char TCHAR;
#define _T(x) x
#define _tcsrchr strrchr
#define _tcschr strchr
#define _tcsdup strdup
#define _tcscpy strcpy
#define _tcslen strlen
#define _tcscmp strcmp
#define _tperror perror
#define _texecvp execvp
#define _tmain main
#define EXECVP_CAST (char **)
#endif
#ifdef _UNICODE
#define TS "%ls"
#else
#define TS "%s"
#endif
static TCHAR *escape(const TCHAR *str) {
#ifdef _WIN32
TCHAR *out = malloc((_tcslen(str) * 2 + 3) * sizeof(*out));
TCHAR *ptr = out;
int i;
*ptr++ = '"';
for (i = 0; str[i]; i++) {
if (str[i] == '"') {
int j = i - 1;
// Before all double quotes, backslashes need to be escaped, but
// not elsewhere.
while (j >= 0 && str[j--] == '\\')
*ptr++ = '\\';
// Escape the next double quote.
*ptr++ = '\\';
}
*ptr++ = str[i];
}
// Any final backslashes, before the quote around the whole argument,
// need to be doubled.
int j = i - 1;
while (j >= 0 && str[j--] == '\\')
*ptr++ = '\\';
*ptr++ = '"';
*ptr++ = '\0';
return out;
#else
return _tcsdup(str);
#endif
}
static TCHAR *concat(const TCHAR *prefix, const TCHAR *suffix) {
int prefixlen = _tcslen(prefix);
int suffixlen = _tcslen(suffix);
TCHAR *buf = malloc((prefixlen + suffixlen + 1) * sizeof(*buf));
_tcscpy(buf, prefix);
_tcscpy(buf + prefixlen, suffix);
return buf;
}
static TCHAR *_tcsrchrs(const TCHAR *str, TCHAR char1, TCHAR char2) {
TCHAR *ptr1 = _tcsrchr(str, char1);
TCHAR *ptr2 = _tcsrchr(str, char2);
if (!ptr1)
return ptr2;
if (!ptr2)
return ptr1;
if (ptr1 < ptr2)
return ptr2;
return ptr1;
}
#ifdef _WIN32
static int filter_line = 0, last_char = '\n';
static void filter_stderr(char *buf, int n) {
// Filter the stderr output from "-v" to rewrite paths from backslash
// to forward slash form. libtool parses the output of "-v" and can't
// handle the backslash form of paths. A proper upstream solution has
// been discussed at https://reviews.llvm.org/D53066 but hasn't been
// finished yet.
int out = 0;
int last = last_char;
for (int i = 0; i < n; i++) {
TCHAR cur = buf[i];
// All lines that contain command lines or paths currently start
// with a space.
if (last == '\n') {
filter_line = cur == ' ';
}
if (filter_line) {
if (cur == '"') {
// Do nothing; skip the quotes. This assumes that after
// converting backslashes to forward slashes, there's nothing
// else (e.g. spaces) that needs quoting. libtool would
// probably not handle that anyway, but this would break
// a more capable caller which also parses the output of "-v".
} else if (cur == '\\') {
// Convert backslashes to forward slashes. Quoted backslashes
// are doubled, so just output every other one. We don't really
// keep track of whether we're in a quoted context though.
if (last != '\\') {
buf[out++] = '/';
} else {
// Last output char was a backslash converted into a
// forward slash. Ignore this one, but handle the next
// one in case there's more.
last = ' ';
}
} else {
buf[out++] = cur;
}
} else {
buf[out++] = cur;
}
last = cur;
}
last_char = last;
fwrite(buf, 1, out, stderr);
}
static int exec_filtered(const TCHAR **argv) {
int len = 1;
for (int i = 0; argv[i]; i++)
len += _tcslen(argv[i]) + 1;
TCHAR *cmdline = malloc(len * sizeof(*cmdline));
int pos = 0;
// On Windows, the arguments are already quoted and escaped properly.
for (int i = 0; argv[i]; i++) {
_tcscpy(&cmdline[pos], argv[i]);
pos += _tcslen(argv[i]);
cmdline[pos++] = ' ';
}
if (pos > 0)
pos--;
cmdline[pos] = '\0';
STARTUPINFO si = { 0 };
PROCESS_INFORMATION pi = { 0 };
HANDLE pipe_read = NULL, pipe_write = NULL;
SECURITY_ATTRIBUTES sa = { 0 };
sa.nLength = sizeof(sa);
sa.bInheritHandle = TRUE;
CreatePipe(&pipe_read, &pipe_write, &sa, 0);
si.cb = sizeof(si);
si.dwFlags = STARTF_USESTDHANDLES;
si.hStdInput = GetStdHandle(STD_INPUT_HANDLE);
si.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE);
si.hStdError = pipe_write;
if (!CreateProcess(NULL, cmdline, NULL, NULL, /* bInheritHandles */ TRUE,
0, NULL, NULL, &si, &pi)) {
DWORD err = GetLastError();
TCHAR *errbuf;
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR) &errbuf, 0, NULL);
_ftprintf(stderr, _T("Unable to execute: "TS": "TS"\n"), cmdline, errbuf);
LocalFree(errbuf);
CloseHandle(pipe_read);
CloseHandle(pipe_write);
free(cmdline);
return 1;
}
CloseHandle(pipe_write);
char stderr_buf[8192];
DWORD n;
while (ReadFile(pipe_read, stderr_buf, sizeof(stderr_buf), &n, NULL))
filter_stderr(stderr_buf, n);
CloseHandle(pipe_read);
WaitForSingleObject(pi.hProcess, INFINITE);
DWORD exit_code = 1;
GetExitCodeProcess(pi.hProcess, &exit_code);
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
free(cmdline);
return exit_code;
}
#endif
int _tmain(int argc, TCHAR* argv[]) {
const TCHAR *argv0 = argv[0];
const TCHAR *sep = _tcsrchrs(argv0, '/', '\\');
TCHAR *dir = _tcsdup(_T(""));
const TCHAR *basename = argv0;
if (sep) {
dir = _tcsdup(argv0);
dir[sep + 1 - argv0] = '\0';
basename = sep + 1;
}
#ifdef _WIN32
TCHAR module_path[8192];
GetModuleFileName(NULL, module_path, sizeof(module_path)/sizeof(module_path[0]));
TCHAR *sep2 = _tcsrchr(module_path, '\\');
if (sep2) {
sep2[1] = '\0';
dir = _tcsdup(module_path);
}
#endif
basename = _tcsdup(basename);
TCHAR *period = _tcschr(basename, '.');
if (period)
*period = '\0';
TCHAR *dash = _tcsrchr(basename, '-');
const TCHAR *target = basename;
const TCHAR *exe = basename;
if (dash) {
*dash = '\0';
exe = dash + 1;
} else {
target = _T(DEFAULT_TARGET);
}
TCHAR *arch = _tcsdup(target);
dash = _tcschr(arch, '-');
if (dash)
*dash = '\0';
// Check if trying to compile Ada; if we try to do this, invoking clang
// would end up invoking <triplet>-gcc with the same arguments, which ends
// up in an infinite recursion.
for (int i = 1; i < argc - 1; i++) {
if (!_tcscmp(argv[i], _T("-x")) && !_tcscmp(argv[i + 1], _T("ada"))) {
fprintf(stderr, "Ada is not supported\n");
return 1;
}
}
int max_arg = argc + 20;