scc

simple c99 compiler
git clone git://git.simple-cc.org/scc
Log | Files | Refs | Submodules | README | LICENSE

commit db0fb021818f369c16fe70bc252fad5461247ef6
parent b8738f479a2f542a5e9f76fd981e0914b607286b
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Wed, 23 Oct 2024 13:02:26 +0200

build: Add config.mk generation

This new config step is optional, but it allows to setup the
variables and enables cross compiling.

Diffstat:
M.gitignore | 2+-
MMakefile | 67++++++++++++++++++-------------------------------------------------
MREADME | 148++++++++++++++++++++++++++++++++++++++++++++++++++-----------------------------
Minclude/bits/scc/Makefile | 17++++-------------
Amain.mk | 56++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mscripts/amd64.mk | 12++++++------
Mscripts/arm.mk | 2+-
Mscripts/arm64.mk | 2+-
Mscripts/config | 86++++++++++++++++++++++++++++++++++++++++++++-----------------------------------
Mscripts/i386.mk | 2+-
Mscripts/ppc.mk | 2+-
Mscripts/rules.mk | 5+----
12 files changed, 231 insertions(+), 170 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -34,4 +34,4 @@ dirs /src/cmd/cc/posix/scc-cpp /src/cmd/ld/scc-ld /src/cmd/make/scc-make -/ +/config.mk diff --git a/Makefile b/Makefile @@ -1,56 +1,25 @@ .POSIX: -DIRS =\ - src\ - src/libc\ - src/libcrt\ - include/bits/scc\ - tests\ +all tests install uninstall: check_config FORCE + +@$(MAKE) -f main.mk $@ -PROJECTDIR = . -include scripts/rules.mk +check_config: FORCE + @if ! test -f config.mk;\ + then\ + $(MAKE) config;\ + fi -ROOT = $(DESTDIR)$(PREFIX) -NODEP = 1 +config: FORCE + ./scripts/config + $(MAKE) -f main.mk config -all: - +@$(MAKE) `$(SCRIPTDIR)/config -c` config - +@$(MAKE) `$(SCRIPTDIR)/config -c` toolchain - +@$(MAKE) `$(SCRIPTDIR)/config` $(ARCH) +clean: FORCE + touch config.mk + $(MAKE) -f main.mk clean -config: - +@cd include/bits/scc && $(MAKE) +distclean: FORCE + touch config.mk + $(MAKE) -f main.mk distclean + rm -f config.mk -install: all - $(SCRIPTDIR)/install $(ROOT) - +@$(MAKE) install-`uname -m` - -uninstall: - $(SCRIPTDIR)/uninstall $(ROOT) - +@$(MAKE) uninstall-`uname -m` - -toolchain: src -libc: src/libc -libcrt: src/libcrt -src: dirs include/bits/scc -src/libc: dirs -src/libcrt: dirs - -dirs: $(SCRIPTDIR)/libc-dirs - xargs mkdir -p < $(SCRIPTDIR)/libc-dirs - touch dirs - -clean: - xargs rm -rf < $(SCRIPTDIR)/libc-dirs - find . -name '*.gcno' -o -name '*.gcda' | xargs rm -f - rm -rf dirs coverage - -distclean: clean - rm -f include/bits/scc/sys.h - rm -f include/bits/scc/config.h - -include scripts/amd64.mk -include scripts/arm.mk -include scripts/arm64.mk -include scripts/i386.mk -include scripts/ppc.mk +FORCE: diff --git a/README b/README @@ -1,91 +1,127 @@ Compiling ========= -SCC is a portable toolchain that can be compiled on any UNIX system -out of the box. It supports several configuration options that -can be passed to the command line: +SCC is a portable toolchain that can be compiled on any UNIX system out +of the box. Compiling the project is so simple like: - - PREFIX: Prefix of the path where scc toolchain is going - to be installed. /usr/local by default. + $ make - - LIBPREFIX: Prefix of the path where scc searchs for - headers and libraries when scc is executed. $PREFIX - by default. +It will build scc selecting the more appropiate options for the host +system. In some situations (like for example when cross compiling), +can be desirable to select custom options for the build and use: - - DESTDIR: Temporary directory prepend to PREFIX used in the - install path. It is mainly intended to help package maintainers - to install in a specific directory used as base for the package - generation. + $ make config + $ make + +and pass the required options the the config target. For example +if we want to cross compile scc for aarch64-linux-musl target +from a amd64-linux-gnu host system: + + $ make config CROSS_COMPILE=aarch-linux-musl- CONF=arm64-linux + $ make + +The build system of scc supports parallel builds that will reduce +dramatically the build time. + +config.mk +--------- - - CROSS_COMPILE: - Specify a prefix name for the tools called by the Makefile. +This file is generated when the config target is built. Once it is +generated it is used by any Makefile in the build system, and it is not +modified by the build system, allowing to the user doing any modifications +as required. This file incules the following options that can be overriden +in the command line of `make config`: - - HOST: - Specify the host system to be used. Possible supported + - TOOL: Specify the toolchain type to be used. Possible + supported values are: + + - clang + - gcov + - gnu + - pcc + - plan9 + - unix + + - HOST: Specify the host system to be used. Possible supported values are: - - unix (by default) - bsd + - linux + - obsd - plan9 + - posix - - CONF: Specify which version of libc to build. - Once the build process completes only the target specified in - CONF will be built. Supported values are: + - CONF: Specify the build configuration used. It determines + the default values for the architecture, ABI, system and + binary format for the cross compiler. It also determines + what versions of the libc are built (at this moment scc + depends of external tools to build the libc and it limits + the versions of the libc that can be built to the ones + supported by the toolchain used to build scc itself). - - amd64-linux (default) - amd64-darwin - - amd64-openbsd - - arm64-linux - amd64-dragonfly - amd64-freebsd + - amd64-linux - amd64-netbsd + - amd64-openbsd + - amd64-plan9 - arm32-linux - - i386-linux + - arm64-linux + - ppc32-linux - Not all the configurations have the same level of support in - the libc and in some cases the support is minimal. + - CROSS_COMPILE: Specify a prefix name for the tools called by the + Makefile. - - TOOL: Specify the toolchain type to be used. Possible - supported values are: + - DESTDIR: Temporary directory prepend to PREFIX used in the + install path. It is mainly intended to help package maintainers + to install in a specific directory used as base for the package + generation. - - unix (by default) - - gnu - - gnu-darwin - - clang - - pcc - - plan9 + - PREFIX: Prefix of the path where scc toolchain is going + to be installed. /usr/local by default. -Beware that the default target selects the appropiate value for HOST, -CONF and TOOL. In case of being needed, TOOL can be still overloaded -in the command line. + - LIBPREFIX: Prefix of the path where scc searchs for + headers and libraries when scc is executed. $PREFIX + by default. + + - LIBPROFILE: The profile used to configure the libc used by the + target compiler. + + - scc: The scc libc. + - scc_clang: The scc libc, but using the clang assembler + and linker. + - musl: The linux musl libc. + + - STD: What version of the C standard is used in the target + compiler and libc. + + - c89: ANSI or C90 ISO standard. + - c99: C99 ISO standard. + +Not all the configurations have the same level of support in +the libc and in some cases the support is minimal. The main targets of the Makefile are: - all: - Compile the toolchain and the libc. It automatically - determines what is the best value for HOST. It sets the - value of CONF for the toolchain that is used by the - toolchain as the default target. It also compiles the libc + Compile the toolchain and the libc. It compiles the libc for all the available configurations based in the host architecture. - - config - Generate headers supposed to be customized by the user. - - - toolchain - Compile the toolchain with the default configuration - specified in CONF. Beware that this target is executed without - the automatic detection of the host parameters. It makes - easier to cross compile. - - - libc: - Compile the libc for the target specified in CONF. Beware - that this target is executed without the automatic detection - of the host parameters. It makes easier to cross compile. + - config: + Generate config.mk and the headers `include/bits/scc/sys.h`, + `include/bits/scc/std.h` and `include/bits/scc/config.h`. + These files are not modified by any other target of the + Makefile, and they can be customized as required after being + generated. They are removed by the `distclean` target. - install: Installs scc in PREFIX. + - uninstall: + Uninstall scc from PREFIX. + - clean: Remove all the generated files except the one supposed to be edited by the user. @@ -160,7 +196,9 @@ depends heavily of the toolchain used to compile musl. As the musl libc is likely installed in a different prefix the scc compilation must be modified to: - $ make LIBPREFIX=/usr/local/musl # point to the prefix used by your musl + $ make LIBPREFIX=/usr/local/musl LIBPROFILE=musl config + +where `LIBPREFIX` points to the prefix used by your musl libc installation. If the helper scc shell script is used instead of scc-cc then the environment variable SCCLIBPREFIX must be set: diff --git a/include/bits/scc/Makefile b/include/bits/scc/Makefile @@ -1,22 +1,16 @@ .POSIX: -PREFIX = /usr/local PROJECTDIR = ../../.. include $(PROJECTDIR)/scripts/rules.mk -SYSHDR =\ - config.h\ - sys.h\ - cstd.h\ - NODEP = 1 -all: $(SYSHDR) +all: config.h sys.h cstd.h -cstd.h: cstd-$(STD).h +cstd.h: cp cstd-$(STD).h $@ -config.h: +config.h: FORCE rm -f $@;\ trap "rm -f $$$$.h" EXIT INT QUIT HUP TERM;\ (echo '#define PREFIX "$(PREFIX)"';\ @@ -26,12 +20,9 @@ config.h: echo '#define LIBPREFIX "$(LIBPREFIX)"';\ echo '#define FORMAT "$(FORMAT)"') > $$$$.h && mv $$$$.h $@ -sys.h: sys-$(LIBPROFILE).h +sys.h: rm -f $@;\ trap "rm -f $$$$.tmp" INT QUIT TERM HUP;\ sed -e 's/%NOPIE%/"$(NOPIE_LDFLAGS)",/' \ -e 's/"",//' sys-$(LIBPROFILE).h > $$$$.tmp && \ mv $$$$.tmp $@ - -clean: - rm -f cstd.h diff --git a/main.mk b/main.mk @@ -0,0 +1,56 @@ +.POSIX: + +DIRS =\ + src\ + src/libc\ + src/libcrt\ + include/bits/scc\ + tests\ + +PROJECTDIR = . +include scripts/rules.mk + +ROOT = $(DESTDIR)$(PREFIX) +NODEP = 1 + +all: + +@$(MAKE) -f main.mk toolchain + +@$(MAKE) -f main.mk $(ARCH) + +config: FORCE + +@cd include/bits/scc && $(MAKE) + +install: all + $(SCRIPTDIR)/install $(ROOT) + +@$(MAKE) install-`uname -m` + +uninstall: + $(SCRIPTDIR)/uninstall $(ROOT) + +@$(MAKE) uninstall-`uname -m` + +toolchain: src +libc: src/libc +libcrt: src/libcrt +src: include/bits/scc + +src src/libc src/libcrt: dirs + +dirs: $(SCRIPTDIR)/libc-dirs + xargs mkdir -p < $(SCRIPTDIR)/libc-dirs + touch dirs + +clean: + xargs rm -rf < $(SCRIPTDIR)/libc-dirs + find . -name '*.gcno' -o -name '*.gcda' | xargs rm -f + rm -rf dirs coverage + +distclean: clean + rm -f include/bits/scc/cstd.h + rm -f include/bits/scc/sys.h + rm -f include/bits/scc/config.h + +include scripts/amd64.mk +include scripts/arm.mk +include scripts/arm64.mk +include scripts/i386.mk +include scripts/ppc.mk diff --git a/scripts/amd64.mk b/scripts/amd64.mk @@ -1,10 +1,10 @@ amd64: - +@$(MAKE) `$(SCRIPTDIR)/config` CONF=amd64-linux libc libcrt - +@$(MAKE) `$(SCRIPTDIR)/config` CONF=amd64-openbsd libc libcrt - +@$(MAKE) `$(SCRIPTDIR)/config` CONF=amd64-netbsd libc libcrt - +@$(MAKE) `$(SCRIPTDIR)/config` CONF=amd64-dragonfly libc libcrt - +@$(MAKE) `$(SCRIPTDIR)/config` CONF=amd64-darwin libc libcrt - +@$(MAKE) `$(SCRIPTDIR)/config` CONF=amd64-freebsd libc libcrt + +@$(MAKE) -f main.mk CONF=amd64-linux libc libcrt + +@$(MAKE) -f main.mk CONF=amd64-openbsd libc libcrt + +@$(MAKE) -f main.mk CONF=amd64-netbsd libc libcrt + +@$(MAKE) -f main.mk CONF=amd64-dragonfly libc libcrt + +@$(MAKE) -f main.mk CONF=amd64-darwin libc libcrt + +@$(MAKE) -f main.mk CONF=amd64-freebsd libc libcrt install-amd64: amd64 $(SCRIPTDIR)/install -p $(SCRIPTDIR)/proto.amd64 $(ROOT) diff --git a/scripts/arm.mk b/scripts/arm.mk @@ -1,5 +1,5 @@ arm: - +@$(MAKE) `$(SCRIPTDIR)/config` CONF=arm-linux libc libcrt + +@$(MAKE) -f main.mk CONF=arm-linux libc libcrt install-arm: arm $(SCRIPTDIR)/install -p $(SCRIPTDIR)/proto.arm $(ROOT) diff --git a/scripts/arm64.mk b/scripts/arm64.mk @@ -1,5 +1,5 @@ arm64: - +@$(MAKE) `$(SCRIPTDIR)/config` CONF=arm64-linux libc libcrt + +@$(MAKE) -f main.mk CONF=arm64-linux libc libcrt install-arm64: arm64 $(SCRIPTDIR)/install -p $(SCRIPTDIR)/proto.arm64 $(ROOT) diff --git a/scripts/config b/scripts/config @@ -1,56 +1,66 @@ #!/bin/sh -case `uname` in -OpenBSD) - echo TOOL=${TOOL:-clang} HOST=obsd ARFLAGS="${ARFLAGS:--rv}" NOPIE_LDFLAGS=-no-pie +exec > $$.tmp +trap "rm -f $$.tmp" EXIT INT TERM + +arch=`uname -m` +sys=`uname -s | tr 'A-Z' 'a-z'` + +case $sys in +openbsd) + echo TOOL=${TOOL:-clang} + echo HOST=obsd + echo ARFLAGS="${ARFLAGS:--rv}" NOPIE_LDFLAGS=-no-pie ;; -NetBSD) +netbsd) #TODO: send a patch to NetBSD to solve this problem - echo TOOL=${TOOL:-gnu} HOST=bsd ARFLAGS="${ARFLAGS:--rv}" + echo TOOL=${TOOL:-gnu} + echo HOST=bsd + echo ARFLAGS="${ARFLAGS:--rv}" ;; -FreeBSD) - echo TOOL=${TOOL:-clang} HOST=bsd +freebsd) + echo TOOL=${TOOL:-clang} + echo HOST=bsd ;; -Darwin) - echo HOST=bsd RANLIB_FLAGS="${RANLIB_FLAGS:--c}" +darwin) + echo HOST=bsd + echo RANLIB_FLAGS="${RANLIB_FLAGS:--c}" ;; -Minix) +minix) echo RANLIB="${RANLIB:-ar t}" ;; -*BSD) +*bsd) echo HOST=bsd ;; -Linux) - echo TOOL=${TOOL:-gnu} HOST=linux +linux) + echo TOOL=${TOOL:-gnu} + echo HOST=linux ;; -Plan9) - echo TOOL=${TOOL:-plan9} HOST=plan9 +plan9) + echo TOOL=${TOOL:-plan9} + echo HOST=plan9 ;; +*) + echo TOOL=${TOOL:-unix} + echo HOST=${HOST:-posix} esac -for i -do - case $1 in - -c) - - mach=`uname -m` +case $arch in +x86_64) + arch=amd64 + ;; +aarch64) + arch=arm64 + ;; +esac - case $mach in - x86_64) - mach=amd64 - ;; - aarch64) - mach=arm64 - ;; - esac +echo CONF=${CONF:-$arch-$sys} +echo CROSS_COMPILE=$CROSS_COMPILE +echo DESTDIR=$DESTDIR +echo PREFIX=${PREFIX:=/usr/local} +echo LIBPREFIX=${LIBPREFIX:-$PREFIX} +echo LIBPROFILE=${LIBPROFILE:-scc} +echo STD=${STD:-c99} - sys=`uname -s | tr 'A-Z' 'a-z'` - echo CONF=$mach-$sys - ;; - *) - echo usage: config [-c] >&2 - exit 1 - ;; - esac -done +mv $$.tmp config.mk diff --git a/scripts/i386.mk b/scripts/i386.mk @@ -1,5 +1,5 @@ i386: - +@$(MAKE) `$(SCRIPTDIR)/config` CONF=i386-linux libc libcrt + +@$(MAKE) -f main.mk CONF=i386-linux libc libcrt install-i386: i386 $(SCRIPTDIR)/install -p $(SCRIPTDIR)/proto.i386 $(ROOT) diff --git a/scripts/ppc.mk b/scripts/ppc.mk @@ -1,5 +1,5 @@ ppc: - +@$(MAKE) `$(SCRIPTDIR)/config` CONF=ppc-linux libc libcrt + +@$(MAKE) -f main.mk CONF=ppc-linux libc libcrt install-ppc: ppc $(SCRIPTDIR)/install -p $(SCRIPTDIR)/proto.ppc $(ROOT) diff --git a/scripts/rules.mk b/scripts/rules.mk @@ -1,7 +1,6 @@ # Define the target all as default all: -# Define default configuration variables PREFIX = /usr/local LIBPREFIX = $(PREFIX) LIBPROFILE = scc @@ -9,6 +8,7 @@ CONF = amd64-linux TOOL = unix HOST = posix ROFF = gnu +include $(PROJECTDIR)/config.mk # Define helper macros for project directories DOCDIR = $(PROJECTDIR)/doc @@ -39,9 +39,6 @@ CPPINCLUDES = -I$(INCDIR)/bits ASINCLUDES = -I$(INCDIR)bits LDINCLUDES = -L$(LIBDIR)/scc -# C standard for the target compiler -STD = c99 - # Definition of command line for cc, as, ld and emu PROJ_CPPFLAGS =\ $(CPPINCLUDES)\