cosmopolitan/build/definitions.mk

430 lines
12 KiB
Makefile
Raw Normal View History

2020-06-15 14:18:57 +00:00
#-*-mode:makefile-gmake;indent-tabs-mode:t;tab-width:8;coding:utf-8-*-┐
#───vi: set et ft=make ts=8 tw=8 fenc=utf-8 :vi───────────────────────┘
#
# SYNOPSIS
#
# Cosmopolitan Core Build Definitions
#
# DESCRIPTION
#
# Nearly all compiler flag tuning is done within this one file. Flags
# may be customized with environment variables. We also use this file
# to compute expensive values once per build.
#
# When tuning the variables below, please note they're interpreted in
# the strictest sense. For example, we don't pass CFLAGS to gcc if we
2021-02-07 14:11:44 +00:00
# know it's compiling a .S file. This enables our `make V=0` logging
# to be succinct and informative at the cost of being less forgiving.
2020-06-15 14:18:57 +00:00
#
# Further note that link order is equally unforgiving in repositories
# of this scale. We approach that by over-specifying dependencies, in
# packages that're broken down usually on a per-directory basis. This
# is aided by the checkdeps and functional programming tools which in
# most cases should be able to deduce correct ordering automatically.
#
# Concerning our approach to flag tuning, most of it is non-essential
# and largely serves to turn features off. Particularly features that
# would otherwise lock us in to a particular platform or legal terms.
# Other flags will usually be the ones that provide us marginal gains
# in terms of performance and code size, but the world won't end when
# they aren't used. Flags that play a critical role in source working
# correctly will usually be specified on a object-by-object basis, in
2020-06-15 14:18:57 +00:00
# their respective packages.
#
# VARIABLES
#
# Our configuration variables, ordered by increasing preference:
#
# CCFLAGS frontend flags (.i, .c, .cc, .f, .S, .lds, etc.)
# OFLAGS objectify flags (precludes -S and -E)
2020-06-15 14:18:57 +00:00
# CPPFLAGS preprocessor flags (.h, .c, .cc, .S, .inc, .lds, etc.)
# TARGET_ARCH microarchitecture flags (e.g. -march=native)
# COPTS c/c++ flags (.c, .cc)
2020-06-15 14:18:57 +00:00
# CFLAGS c flags (.c only)
# CXXFLAGS c++ flags (.cc only)
# LDFLAGS linker flags (don't use -Wl, frontend prefix)
# ASFLAGS assembler flags (don't use -Wa, frontend prefix)
#
# For each FOO above, there exists (by increasing preference)
#
# DEFAULT_FOO see build/definitions.mk
# CONFIG_FOO see build/config.mk
# FOO set ~/.cosmo.mk and target-specific
# OVERRIDE_FOO set ~/.cosmo.mk and target-specific (use rarely)
#
2020-06-15 14:18:57 +00:00
LC_ALL = C
2021-02-07 14:11:44 +00:00
SOURCE_DATE_EPOCH = 0
2020-06-15 14:18:57 +00:00
ARFLAGS = rcsD
ZFLAGS ?=
2020-06-15 14:18:57 +00:00
XARGS ?= xargs -P4 -rs8000
DOT ?= dot
2021-02-22 07:33:44 +00:00
CLANG = clang
2020-06-15 14:18:57 +00:00
FC = gfortran #/opt/cross9f/bin/x86_64-linux-musl-gfortran
TMPDIR = o/tmp
2020-06-15 14:18:57 +00:00
AR = build/bootstrap/ar.com
CP = build/bootstrap/cp.com
RM = build/bootstrap/rm.com -f
GZIP = build/bootstrap/gzip.com
ECHO = build/bootstrap/echo.com
2023-06-08 12:17:28 +00:00
CHMOD = build/bootstrap/chmod.com
TOUCH = build/bootstrap/touch.com
PKG = build/bootstrap/package.com
MKDEPS = build/bootstrap/mkdeps.com
ZIPOBJ = build/bootstrap/zipobj.com
ZIPCOPY = build/bootstrap/zipcopy.com
PECHECK = build/bootstrap/pecheck.com
Prove that Makefile is fully defined The whole repository is now buildable with GNU Make Landlock sandboxing. This proves that no Makefile targets exist which touch files other than their declared prerequisites. In order to do this, we had to: 1. Stop code morphing GCC output in package.com and instead run a newly introduced FIXUPOBJ.COM command after GCC invocations. 2. Disable all the crumby Python unit tests that do things like create files in the current directory, or rename() files between folders. This ended up being a lot of tests, but most of them are still ok. 3. Introduce an .UNSANDBOXED variable to GNU Make to disable Landlock. We currently only do this for things like `make tags`. 4. This change deletes some GNU Make code that was preventing the execve() optimization from working. This means it should no longer be necessary in most cases for command invocations to be indirected through the cocmd interpreter. 5. Missing dependencies had to be declared in certain places, in cases where they couldn't be automatically determined by MKDEPS.COM 6. The libcxx header situation has finally been tamed. One of the things that makes this difficult is MKDEPS.COM only wants to consider the first 64kb of a file, in order to go fast. But libcxx likes to have #include lines buried after huge documentation. 7. An .UNVEIL variable has been introduced to GNU Make just in case we ever wish to explicitly specify additional things that need to be whitelisted which aren't strictly prerequisites. This works in a manner similar to the recently introduced .EXTRA_PREREQS feature. There's now a new build/bootstrap/make.com prebuilt binary available. It should no longer be possible to write invalid Makefile code.
2022-08-06 10:51:50 +00:00
FIXUPOBJ = build/bootstrap/fixupobj.com
MKDIR = build/bootstrap/mkdir.com -p
COMPILE = build/bootstrap/compile.com -V9 -P4096 $(QUOTA)
COMMA := ,
PWD := $(shell build/bootstrap/pwd.com)
IGNORE := $(shell $(MKDIR) $(TMPDIR))
ifneq ($(findstring aarch64,$(MODE)),)
ARCH = aarch64
VM = o/third_party/qemu/qemu-aarch64
HOSTS ?= pi silicon
else
ARCH = x86_64
2023-09-10 15:12:43 +00:00
HOSTS ?= freebsd rhel7 rhel5 xnu win10 openbsd netbsd
endif
ifeq ($(PREFIX),)
ifeq ($(USE_SYSTEM_TOOLCHAIN),)
ifneq ("$(wildcard o/third_party/gcc/bin/x86_64-pc-linux-gnu-*)","")
PREFIX = o/third_party/gcc/bin/x86_64-pc-linux-gnu-
else
IGNORE := $(shell build/bootstrap/unbundle.com)
PREFIX = o/third_party/gcc/bin/x86_64-linux-musl-
2023-05-02 02:43:59 +00:00
endif
ifeq ($(ARCH), aarch64)
2023-05-02 02:43:59 +00:00
PREFIX = o/third_party/gcc/bin/aarch64-linux-musl-
endif
endif
endif
2020-06-15 14:18:57 +00:00
AS = $(PREFIX)as
CC = $(PREFIX)gcc
CXX = $(PREFIX)g++
CXXFILT = $(PREFIX)c++filt
LD = $(PREFIX)ld.bfd
NM = $(PREFIX)nm
GCC = $(PREFIX)gcc
STRIP = $(PREFIX)strip
OBJCOPY = $(PREFIX)objcopy
OBJDUMP = $(PREFIX)objdump
ifneq ($(wildcard $(PWD)/$(PREFIX)addr2line), )
ADDR2LINE = $(PWD)/$(PREFIX)addr2line
else
ADDR2LINE = $(PREFIX)addr2line
endif
2020-06-15 14:18:57 +00:00
export ADDR2LINE
2021-02-07 14:11:44 +00:00
export LC_ALL
export MKDIR
2020-06-15 14:18:57 +00:00
export MODE
2021-02-07 14:11:44 +00:00
export SOURCE_DATE_EPOCH
2020-06-15 14:18:57 +00:00
export TMPDIR
2021-02-07 14:11:44 +00:00
ifeq ($(LANDLOCKMAKE_VERSION),)
TMPSAFE = $(join $(TMPDIR),$(subst /,_,$@)).tmp
else
TMPSAFE = $(TMPDIR)/
endif
BACKTRACES = \
-fno-optimize-sibling-calls \
-mno-omit-leaf-frame-pointer
ifneq ($(ARCH), aarch64)
BACKTRACES += -fno-schedule-insns2
endif
2020-06-15 14:18:57 +00:00
SANITIZER = \
2021-02-07 14:11:44 +00:00
-fsanitize=address
2020-06-15 14:18:57 +00:00
NO_MAGIC = \
-ffreestanding \
2020-06-15 14:18:57 +00:00
-fno-stack-protector \
-fwrapv \
-fno-sanitize=all \
-fpatchable-function-entry=0,0
2020-06-15 14:18:57 +00:00
OLD_CODE = \
-fno-strict-aliasing \
-fno-strict-overflow
TRADITIONAL = \
-Wno-implicit-int \
-Wno-return-type \
-Wno-pointer-sign
DEFAULT_CCFLAGS += \
2020-06-15 14:18:57 +00:00
-Wall \
-Werror \
-fno-omit-frame-pointer \
2020-06-15 14:18:57 +00:00
-frecord-gcc-switches
DEFAULT_COPTS ?= \
2020-06-15 14:18:57 +00:00
-fno-math-errno \
-fno-ident \
-fno-common \
-fno-gnu-unique \
-fstrict-aliasing \
-fstrict-overflow \
-fno-semantic-interposition \
-fno-dwarf2-cfi-asm \
-fno-unwind-tables \
-fno-asynchronous-unwind-tables
2023-05-01 21:36:25 +00:00
ifeq ($(ARCH), x86_64)
# Microsoft says "[a]ny memory below the stack beyond the red zone
# [note: Windows defines the x64 red zone size as 0] is considered
# volatile and may be modified by the operating system at any time."
# https://devblogs.microsoft.com/oldnewthing/20190111-00/?p=100685
2023-05-01 21:36:25 +00:00
DEFAULT_COPTS += \
-mno-red-zone \
-mno-tls-direct-seg-refs
2023-05-01 21:36:25 +00:00
endif
2020-06-15 14:18:57 +00:00
ifeq ($(ARCH), aarch64)
#
# - Apple says in "Writing ARM64 code for Apple platforms" that we're
# not allowed to use the x18 register.
#
# - Cosmopolitan Libc uses x28 for thread-local storage because Apple
# forbids us from using tpidr_el0 too.
#
# - Cosmopolitan currently lacks an implementation of the runtime
# libraries needed by the -moutline-atomics flag
#
DEFAULT_COPTS += \
-ffixed-x18 \
-ffixed-x28 \
-mno-outline-atomics
endif
MATHEMATICAL = \
-O3 \
-fwrapv
DEFAULT_CPPFLAGS += \
-D_COSMO_SOURCE \
-DMODE='"$(MODE)"' \
2020-06-15 14:18:57 +00:00
-nostdinc \
Prove that Makefile is fully defined The whole repository is now buildable with GNU Make Landlock sandboxing. This proves that no Makefile targets exist which touch files other than their declared prerequisites. In order to do this, we had to: 1. Stop code morphing GCC output in package.com and instead run a newly introduced FIXUPOBJ.COM command after GCC invocations. 2. Disable all the crumby Python unit tests that do things like create files in the current directory, or rename() files between folders. This ended up being a lot of tests, but most of them are still ok. 3. Introduce an .UNSANDBOXED variable to GNU Make to disable Landlock. We currently only do this for things like `make tags`. 4. This change deletes some GNU Make code that was preventing the execve() optimization from working. This means it should no longer be necessary in most cases for command invocations to be indirected through the cocmd interpreter. 5. Missing dependencies had to be declared in certain places, in cases where they couldn't be automatically determined by MKDEPS.COM 6. The libcxx header situation has finally been tamed. One of the things that makes this difficult is MKDEPS.COM only wants to consider the first 64kb of a file, in order to go fast. But libcxx likes to have #include lines buried after huge documentation. 7. An .UNVEIL variable has been introduced to GNU Make just in case we ever wish to explicitly specify additional things that need to be whitelisted which aren't strictly prerequisites. This works in a manner similar to the recently introduced .EXTRA_PREREQS feature. There's now a new build/bootstrap/make.com prebuilt binary available. It should no longer be possible to write invalid Makefile code.
2022-08-06 10:51:50 +00:00
-iquote .
2020-06-15 14:18:57 +00:00
DEFAULT_CFLAGS = \
-std=gnu2x
DEFAULT_CXXFLAGS = \
-fno-rtti \
-fno-exceptions \
-fuse-cxa-atexit \
-Wno-int-in-bool-context \
-Wno-narrowing \
-Wno-literal-suffix
2020-06-15 14:18:57 +00:00
DEFAULT_ASFLAGS = \
-W \
-I. \
2023-07-10 19:17:18 +00:00
--noexecstack
2020-06-15 14:18:57 +00:00
DEFAULT_LDFLAGS = \
-static \
-nostdlib \
-znorelro \
2021-02-11 16:37:18 +00:00
--gc-sections \
2020-06-15 14:18:57 +00:00
--build-id=none \
--no-dynamic-linker
# # generate linker report files
# DEFAULT_LDFLAGS += --cref -Map=$@.map
ifeq ($(ARCH), aarch64)
DEFAULT_LDFLAGS += \
-zmax-page-size=0x4000 \
-zcommon-page-size=0x4000 \
-znorelro
else
DEFAULT_LDFLAGS += \
2023-08-17 18:12:10 +00:00
-zmax-page-size=0x4000 \
-zcommon-page-size=0x1000
endif
2020-06-15 14:18:57 +00:00
ASONLYFLAGS = \
-c \
-g
2020-06-15 14:18:57 +00:00
DEFAULT_LDLIBS =
MCA = llvm-mca-10 \
-mtriple=x86_64-pc-linux-gnu \
-instruction-info \
-iterations=3 \
-all-stats \
-all-views \
-timeline
cc.flags = \
$(DEFAULT_CCFLAGS) \
$(CONFIG_CCFLAGS) \
$(CCFLAGS) \
$(OVERRIDE_CCFLAGS)
2020-06-15 14:18:57 +00:00
o.flags = \
$(DEFAULT_OFLAGS) \
$(CONFIG_OFLAGS) \
$(OFLAGS) \
$(OVERRIDE_OFLAGS)
cpp.flags = \
$(DEFAULT_CPPFLAGS) \
$(CONFIG_CPPFLAGS) \
$(CPPFLAGS) \
$(OVERRIDE_CPPFLAGS) \
Prove that Makefile is fully defined The whole repository is now buildable with GNU Make Landlock sandboxing. This proves that no Makefile targets exist which touch files other than their declared prerequisites. In order to do this, we had to: 1. Stop code morphing GCC output in package.com and instead run a newly introduced FIXUPOBJ.COM command after GCC invocations. 2. Disable all the crumby Python unit tests that do things like create files in the current directory, or rename() files between folders. This ended up being a lot of tests, but most of them are still ok. 3. Introduce an .UNSANDBOXED variable to GNU Make to disable Landlock. We currently only do this for things like `make tags`. 4. This change deletes some GNU Make code that was preventing the execve() optimization from working. This means it should no longer be necessary in most cases for command invocations to be indirected through the cocmd interpreter. 5. Missing dependencies had to be declared in certain places, in cases where they couldn't be automatically determined by MKDEPS.COM 6. The libcxx header situation has finally been tamed. One of the things that makes this difficult is MKDEPS.COM only wants to consider the first 64kb of a file, in order to go fast. But libcxx likes to have #include lines buried after huge documentation. 7. An .UNVEIL variable has been introduced to GNU Make just in case we ever wish to explicitly specify additional things that need to be whitelisted which aren't strictly prerequisites. This works in a manner similar to the recently introduced .EXTRA_PREREQS feature. There's now a new build/bootstrap/make.com prebuilt binary available. It should no longer be possible to write invalid Makefile code.
2022-08-06 10:51:50 +00:00
-include libc/integral/normalize.inc
2020-06-15 14:18:57 +00:00
copt.flags = \
$(TARGET_ARCH) \
$(DEFAULT_COPTS) \
$(CONFIG_COPTS) \
$(COPTS) \
$(OVERRIDE_COPTS)
f.flags = \
$(DEFAULT_FFLAGS) \
$(CONFIG_FFLAGS) \
$(FFLAGS) \
$(OVERRIDE_FFLAGS)
c.flags = \
$(DEFAULT_CFLAGS) \
$(CONFIG_CFLAGS) \
$(CFLAGS) \
$(OVERRIDE_CFLAGS)
cxx.flags = \
$(DEFAULT_CXXFLAGS) \
$(CONFIG_CXXFLAGS) \
$(CXXFLAGS) \
$(OVERRIDE_CXXFLAGS)
s.flags = \
$(DEFAULT_ASFLAGS) \
$(CONFIG_ASFLAGS) \
$(ASFLAGS) \
$(OVERRIDE_ASFLAGS)
S.flags = $(addprefix -Wa$(COMMA),$(s.flags))
LD.libs = \
$(LDLIBS) \
$(LOADLIBES) \
$(DEFAULT_LDLIBS) \
$(CONFIG_LDLIBS) \
$(LDLIBS) \
$(DEFAULT_LIBS) \
$(CONFIG_LIBS) \
$(LIBS)
COMPILE.c.flags = $(cc.flags) $(copt.flags) $(cpp.flags) $(c.flags)
COMPILE.cxx.flags = $(cc.flags) $(copt.flags) $(cpp.flags) $(cxx.flags)
2020-06-15 14:18:57 +00:00
COMPILE.f.flags = $(cc.flags) $(copt.flags) $(f.flags)
COMPILE.F.flags = $(cc.flags) $(copt.flags) $(cpp.flags) $(f.flags)
2020-06-15 14:18:57 +00:00
COMPILE.i.flags = $(cc.flags) $(copt.flags) $(c.flags)
COMPILE.ii.flags = $(cc.flags) $(copt.flags) $(cxx.flags)
LINK.flags = $(DEFAULT_LDFLAGS) $(CONFIG_LDFLAGS) $(LDFLAGS)
OBJECTIFY.c.flags = $(cc.flags) $(o.flags) $(S.flags) $(cpp.flags) $(copt.flags) $(c.flags)
OBJECTIFY.cxx.flags = $(cc.flags) $(o.flags) $(S.flags) $(cpp.flags) $(copt.flags) $(cxx.flags)
2020-06-15 14:18:57 +00:00
OBJECTIFY.s.flags = $(ASONLYFLAGS) $(s.flags)
OBJECTIFY.S.flags = $(cc.flags) $(o.flags) $(S.flags) $(cpp.flags)
OBJECTIFY.f.flags = $(cc.flags) $(o.flags) $(S.flags) $(f.flags)
OBJECTIFY.F.flags = $(cc.flags) $(o.flags) $(S.flags) $(cpp.flags) $(copt.flags) $(f.flags)
2020-06-15 14:18:57 +00:00
PREPROCESS.flags = -E $(copt.flags) $(cc.flags) $(cpp.flags)
PREPROCESS.lds.flags = -D__LINKER__ $(filter-out -g%,$(PREPROCESS.flags)) -P -xc
COMPILE.c = $(CC) -S $(COMPILE.c.flags)
COMPILE.cxx = $(CXX) -S $(COMPILE.cxx.flags)
2020-06-15 14:18:57 +00:00
COMPILE.i = $(CC) -S $(COMPILE.i.flags)
COMPILE.f = $(FC) -S $(COMPILE.f.flags)
COMPILE.F = $(FC) -S $(COMPILE.F.flags)
OBJECTIFY.s = $(AS) $(OBJECTIFY.s.flags)
OBJECTIFY.S = $(CC) $(OBJECTIFY.S.flags) -c
OBJECTIFY.f = $(FC) $(OBJECTIFY.f.flags) -c
OBJECTIFY.F = $(FC) $(OBJECTIFY.F.flags) -c
OBJECTIFY.c = $(CC) $(OBJECTIFY.c.flags) -c
OBJECTIFY.cxx = $(CXX) $(OBJECTIFY.cxx.flags) -c
PREPROCESS = $(CC) $(PREPROCESS.flags)
PREPROCESS.lds = $(CC) $(PREPROCESS.lds.flags)
LINK = $(LD) $(LINK.flags)
2020-06-15 14:18:57 +00:00
ELF = o/libc/elf/elf.lds
ELFLINK = $(COMPILE) -ALINK.elf $(LINK) $(LINKARGS) $(OUTPUT_OPTION) && $(COMPILE) -AFIXUP.ape -T$@ $(FIXUPOBJ) $@
2020-06-15 14:18:57 +00:00
LINKARGS = $(patsubst %.lds,-T %.lds,$(call uniqr,$(LD.libs) $(filter-out %.pkg,$^)))
# The compiler won't generate %xmm code for sources extensioned .greg.c,
# which is needed for C modules wanting to run at the executive level or
# during privileged runtime states, e.g. code morphing.
OBJECTIFY.greg.c = \
$(CC) \
$(filter-out -pg,$(OBJECTIFY.c.flags)) \
-D__MGENERAL_REGS_ONLY__ \
-mgeneral-regs-only \
-fno-stack-protector \
-fno-instrument-functions \
-fno-optimize-sibling-calls \
-fno-sanitize=all \
-ffreestanding \
-fwrapv \
2020-06-15 14:18:57 +00:00
-c
OBJECTIFY.ansi.c = $(CC) $(OBJECTIFY.c.flags) -ansi -Wextra -Werror -pedantic-errors -c
OBJECTIFY.c99.c = $(CC) $(OBJECTIFY.c.flags) -std=c99 -Wextra -Werror -pedantic-errors -c
OBJECTIFY.c11.c = $(CC) $(OBJECTIFY.c.flags) -std=c11 -Wextra -Werror -pedantic-errors -c
OBJECTIFY.c2x.c = $(CC) $(OBJECTIFY.c.flags) -std=c2x -Wextra -Werror -pedantic-errors -c
OBJECTIFY.ncabi.c = \
$(GCC) \
$(OBJECTIFY.c.flags) \
-mno-sse \
-mfpmath=387 \
-fno-stack-protector \
-fno-instrument-functions \
-fno-optimize-sibling-calls \
-fno-sanitize=all \
-fcall-saved-rcx \
-fcall-saved-rdx \
-fcall-saved-rdi \
-fcall-saved-rsi \
-fcall-saved-r8 \
-fcall-saved-r9 \
-fcall-saved-r10 \
-fcall-saved-r11 \
-c \
-xc
OBJECTIFY.initabi.c = \
$(GCC) \
$(OBJECTIFY.c.flags) \
-fno-stack-protector \
-fno-instrument-functions \
-fno-optimize-sibling-calls \
-fno-sanitize=all \
-fcall-saved-rdi \
-fcall-saved-rsi \
-c
TAGSFLAGS = \
-e \
-a \
--if0=no \
--langmap=c:.c.h.i \
--line-directives=yes \
--exclude=libc/nt/struct/imagefileheader.internal.h \
--exclude=libc/nt/struct/filesegmentelement.h