Skip to content

Commit 6ac3d3b

Browse files
committed
Add precompiled header support to cosmocc
1 parent f8cfc89 commit 6ac3d3b

File tree

2 files changed

+102
-17
lines changed

2 files changed

+102
-17
lines changed

tool/cosmocc/bin/cosmocc

Lines changed: 56 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,12 @@ elif [ ! -d "$TMPDIR" ]; then
7575
fi
7676
fi
7777

78+
X=
7879
OPT=
7980
ARGS=
8081
FLAGS=
8182
OUTPUT=
83+
NEED_X=
8284
MDFLAG=0
8385
MCOSMO=0
8486
INTENT=ld
@@ -94,8 +96,7 @@ NEED_DEPENDENCY_OUTPUT=
9496
for x; do
9597
if [ x"$x" != x"${x#* }" ]; then
9698
fatal_error "arguments containing spaces unsupported: $x"
97-
fi
98-
if [ -n "$NEED_OUTPUT" ]; then
99+
elif [ -n "$NEED_OUTPUT" ]; then
99100
NEED_OUTPUT=
100101
OUTPUT=$x
101102
continue
@@ -109,6 +110,10 @@ for x; do
109110
elif [ -n "$NEED_EQUAL" ]; then
110111
x="${NEED_EQUAL}=${x}"
111112
NEED_EQUAL=
113+
elif [ -n "$NEED_X" ]; then
114+
NEED_X=
115+
X=$x
116+
x="-x${x}"
112117
elif [ x"$x" = x"-" ] || # is alias for stdin
113118
[ x"$x" = x"${x#-*}" ]; then # !startswith(x, "-")
114119
if [ x"$x" != x"${x%.s}" ] ||
@@ -142,6 +147,7 @@ for x; do
142147
if [ x"$INTENT" != x"cpp" ]; then
143148
INTENT=cc
144149
fi
150+
continue
145151
elif [ x"$x" = x"-E" ] ||
146152
[ x"$x" = x"-M" ] ||
147153
[ x"$x" = x"-MM" ]; then
@@ -214,8 +220,12 @@ for x; do
214220
elif [ x"$x" = x"-dumpversion" ]; then
215221
echo $GCC_VERSION
216222
Exit 0
217-
elif [ x"$x" = x"-x" ] ||
218-
[ x"$x" = x"-e" ] ||
223+
elif [ x"$x" = x"-x" ]; then
224+
NEED_X=1
225+
continue
226+
elif [ x"$x" != x"${x#-x}" ]; then
227+
X=${x#-x}
228+
elif [ x"$x" = x"-e" ] ||
219229
[ x"$x" = x"-z" ] ||
220230
[ x"$x" = x"-T" ] ||
221231
[ x"$x" = x"-L" ] ||
@@ -240,12 +250,37 @@ for x; do
240250
ARGS="$ARGS $x"
241251
done
242252

253+
# precompiled header mode
254+
if [ $INTENT != cpp ]; then
255+
if [ -z "$X" ]; then
256+
ONLY_HEADER_INPUTS=1
257+
for x in $ARGS; do
258+
if [ x"$x" = x"${x#-*}" ] && # !startswith(x, "-")
259+
[ x"$x" = x"${x%.h}" ] && # !endswith(x, ".h")
260+
[ x"$x" = x"${x%.hpp}" ]; then # !endswith(x, ".hpp")
261+
ONLY_HEADER_INPUTS=0
262+
break
263+
fi
264+
done
265+
if [ $ONLY_HEADER_INPUTS -eq 1 ]; then
266+
INTENT=gch
267+
fi
268+
elif [ x"$X" = x"c-header" ] ||
269+
[ x"$X" = x"c++-header" ]; then
270+
INTENT=gch
271+
fi
272+
fi
273+
if [ $INTENT = gch ]; then
274+
fatal_error "precompiled headers only supported with ARCH-unknown-cosmo-cc compilers"
275+
fi
276+
277+
# check for incorrect usage
243278
if [ $INPUT_FILE_COUNT -eq 0 ]; then
244279
fatal_error "no input files"
245-
elif [ -z "$INPUT" ] &&
246-
[ $INTENT != ld ] &&
247-
[ $INPUT_FILE_COUNT -gt 1 ]; then
248-
fatal_error "cannot specify '-o' with '-c', or '-E' with multiple files"
280+
elif [ -n "$OUTPUT" ] && [ $INPUT_FILE_COUNT -gt 1 ]; then
281+
if [ $INTENT = cc ] || [ $INTENT = cpp ]; then
282+
fatal_error "cannot specify '-o' with '-c' or '-E' with multiple files"
283+
fi
249284
fi
250285

251286
PLATFORM="-D__COSMOPOLITAN__ -D__COSMOCC__ -D__FATCOSMOCC__"
@@ -263,12 +298,22 @@ if [ x"$OPT" != x"-O3" ] && [ x"$MODE" != x"optlinux" ]; then
263298
CFLAGS="$CFLAGS -fno-schedule-insns2"
264299
fi
265300

301+
if [ x"$X" = x"c" ] || [ x"$X" = x"c-header" ]; then
302+
CPLUSPLUS=0
303+
elif [ x"$X" = x"c++" ] || [ x"$X" = x"c++-header" ]; then
304+
CPLUSPLUS=1
305+
elif [ x"$PROG" != x"${PROG%++}" ]; then
306+
CPLUSPLUS=1
307+
else
308+
CPLUSPLUS=0
309+
fi
310+
266311
CC_X86_64="$BIN/x86_64-linux-cosmo-gcc"
267312
CC_AARCH64="$BIN/aarch64-linux-cosmo-gcc"
268-
if [ x"$PROG" != x"${PROG%++}" ]; then
313+
if [ $CPLUSPLUS -eq 1 ]; then
269314
CC_X86_64="$BIN/x86_64-linux-cosmo-g++"
270315
CC_AARCH64="$BIN/aarch64-linux-cosmo-g++"
271-
if [ x"$INTENT" != x"cpp" ]; then
316+
if [ $INTENT != cpp ]; then
272317
CFLAGS="$CFLAGS -fno-rtti -fno-exceptions -fuse-cxa-atexit"
273318
fi
274319
CPPFLAGS="-isystem $BIN/../include/third_party/libcxx $CPPFLAGS"
@@ -313,7 +358,7 @@ if [ x"$OPT" != x"-Os" ] && [ x"$MODE" != x"tiny" ] && [ x"$MODE" != x"optlinux"
313358
CFLAGS_AARCH64="${CFLAGS_AARCH64} -fpatchable-function-entry=7,6 -fno-inline-functions-called-once -DFTRACE -DSYSDEBUG"
314359
fi
315360

316-
if [ x"$PROG" != x"${PROG%++}" ]; then
361+
if [ $CPLUSPLUS -eq 1 ]; then
317362
LDLIBS_X86_64="-lcxx ${LDLIBS_X86_64}"
318363
LDLIBS_AARCH64="-lcxx ${LDLIBS_AARCH64}"
319364
fi

tool/cosmocc/bin/cosmocross

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,17 @@ if [ x"$ARCH" = x"$PROG" ]; then
5959
fatal_error "cosmocross must be run via cross compiler"
6060
fi
6161

62+
X=
63+
NEED_X=
6264
for x; do
63-
if [ x"$x" = x"-mtiny" ]; then
65+
if [ -n "$NEED_X" ]; then
66+
NEED_X=
67+
X=$x
68+
elif [ x"$x" = x"-x" ]; then
69+
NEED_X=1
70+
elif [ x"$x" != x"${x#-x}" ]; then
71+
X=${x#-x}
72+
elif [ x"$x" = x"-mtiny" ]; then
6473
MODE=tiny
6574
elif [ x"$x" = x"-mdbg" ]; then
6675
MODE=dbg
@@ -79,16 +88,26 @@ else
7988
LIB="$BIN/../$ARCH-linux-cosmo/lib"
8089
fi
8190

91+
if [ x"$X" = x"c" ] || [ x"$X" = x"c-header" ]; then
92+
CPLUSPLUS=0
93+
elif [ x"$X" = x"c++" ] || [ x"$X" = x"c++-header" ]; then
94+
CPLUSPLUS=1
95+
elif [ x"$PROG" != x"${PROG%++}" ]; then
96+
CPLUSPLUS=1
97+
else
98+
CPLUSPLUS=0
99+
fi
100+
82101
CC="$BIN/$ARCH-linux-cosmo-gcc"
83102
CRT="$LIB/crt.o"
84103
LDLIBS="-lcosmo"
85104
if [ -z "$COSMOS" ]; then
86-
LDFLAGS="$LDFLAGS -L$LIB -L$BIN/../$ARCH-linux-cosmo/lib"
105+
LDFLAGS="$LDFLAGS -L$LIB -L$BIN/../$ARCH-linux-cosmo/lib"
87106
else
88-
LDFLAGS="$LDFLAGS -L$COSMOS/lib -L$LIB -L$BIN/../$ARCH-linux-cosmo/lib"
89-
CPPFLAGS="$CPPFLAGS -I$COSMOS/include"
107+
LDFLAGS="$LDFLAGS -L$COSMOS/lib -L$LIB -L$BIN/../$ARCH-linux-cosmo/lib"
108+
CPPFLAGS="$CPPFLAGS -I$COSMOS/include"
90109
fi
91-
if [ x"$PROG" != x"${PROG%++}" ]; then
110+
if [ $CPLUSPLUS -eq 1 ]; then
92111
CC="$BIN/$ARCH-linux-cosmo-g++"
93112
CFLAGS="$CFLAGS -fno-rtti -fno-exceptions -fuse-cxa-atexit"
94113
CPPFLAGS="-isystem $BIN/../include/third_party/libcxx $CPPFLAGS"
@@ -219,6 +238,27 @@ if [ $RELOCATABLE -eq 1 ]; then
219238
LDFLAGS="$LDFLAGS -r"
220239
fi
221240

241+
# precompiled header mode
242+
if [ $INTENT != cpp ]; then
243+
if [ -z "$X" ]; then
244+
ONLY_HEADER_INPUTS=1
245+
for x; do
246+
if [ x"$x" = x"${x#-*}" ] && # !startswith(x, "-")
247+
[ x"$x" = x"${x%.h}" ] && # !endswith(x, ".h")
248+
[ x"$x" = x"${x%.hpp}" ]; then # !endswith(x, ".hpp")
249+
ONLY_HEADER_INPUTS=0
250+
break
251+
fi
252+
done
253+
if [ $ONLY_HEADER_INPUTS -eq 1 ]; then
254+
INTENT=h
255+
fi
256+
elif [ x"$X" = x"c-header" ] ||
257+
[ x"$X" = x"c++-header" ]; then
258+
INTENT=h
259+
fi
260+
fi
261+
222262
# support --ftrace unless optimizing for size
223263
if [ x"$OPT" != x"-Os" ] && # $OPT != -Os
224264
[ x"$MODE" != x"optlinux" ] && # $MODE not optlinux
@@ -242,7 +282,7 @@ fi
242282

243283
if [ $INTENT = cpp ]; then
244284
set -- "$CC" $PLATFORM $CPPFLAGS "$@"
245-
elif [ $INTENT = cc ] || [ $INTENT = s ]; then
285+
elif [ $INTENT = cc ] || [ $INTENT = s ] || [ $INTENT = h ]; then
246286
set -- "$CC" $PLATFORM $PREDEF $CFLAGS $CPPFLAGS "$@" $PRECIOUS
247287
else
248288
set -- "$CC" $PLATFORM $PREDEF $CFLAGS $CPPFLAGS $CRT "$@" $LDFLAGS $LDLIBS $PRECIOUS

0 commit comments

Comments
 (0)