Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
152 commits
Select commit Hold shift + click to select a range
503ba7b
start moving `zig cc` to stage2
andrewrk Sep 4, 2020
71687b3
work around stage1 invalid LLVM IR
andrewrk Sep 4, 2020
f064f05
stage2: improve log message format
andrewrk Sep 4, 2020
472ee18
stage2: infer --name in more ways
andrewrk Sep 4, 2020
4056bb9
stage2: more progress moving `zig cc` to stage2
andrewrk Sep 8, 2020
35f334a
organize some TODO comments
andrewrk Sep 8, 2020
c99e34a
stage2: eliminate the "compiler id" concept
andrewrk Sep 8, 2020
193ad41
stage2: compiling C objects with clang
andrewrk Sep 9, 2020
b37955f
stage2 linker code supports opening an intermediate object file
andrewrk Sep 9, 2020
5746a86
stage1: link: fix incorrect LDM option for mips64
andrewrk Sep 10, 2020
e05ecbf
stage2: progress towards LLD linking
andrewrk Sep 10, 2020
98583be
stage1: fix crash with slice type
andrewrk Sep 11, 2020
8cf40f3
stage2: loading glibc metadata
andrewrk Sep 11, 2020
c903dad
fix zig_llvm.h not complying with C
andrewrk Sep 12, 2020
8374be1
std.cache_hash: bump up the base64 digest len to avoid '=='
andrewrk Sep 12, 2020
03a2341
stage2: linking with LLD and building glibc static CRT files
andrewrk Sep 12, 2020
af4cc20
Merge remote-tracking branch 'origin/master' into stage2-zig-cc
andrewrk Sep 12, 2020
1baa56a
std.cache_hash: break up the API and improve implementation
andrewrk Sep 14, 2020
2a8fc1a
stage2: caching system integration & Module/Compilation splitting
andrewrk Sep 14, 2020
4d59f77
stage2: rename Module to Compilation
andrewrk Sep 14, 2020
2456df5
stage2: rename ZigModule to Module
andrewrk Sep 14, 2020
046dce9
stage2: fix not creating cache o dir before writing to it
andrewrk Sep 14, 2020
2627778
stage2: don't create empty object files when no zig source
andrewrk Sep 14, 2020
97ea5da
stage2: fix bad include path for glibc abi-note.S
andrewrk Sep 14, 2020
e5aef96
stage2: CRT files retain locks on the build artifacts
andrewrk Sep 14, 2020
0379d7b
stage2: don't bother building glibc when only doing build-obj
andrewrk Sep 14, 2020
060c91b
stage2: namespace cache dir with C source path
andrewrk Sep 14, 2020
f2e3803
stage2: building glibc Scrt1.o
andrewrk Sep 14, 2020
c58e995
revert bogus `enum` keywords in zig_llvm.h
andrewrk Sep 14, 2020
04f6a26
fix stage1 regressions in this branch
andrewrk Sep 14, 2020
778bb4b
move std.cache_hash from std to stage2
andrewrk Sep 14, 2020
40cb712
disable sourcehut freebsd CI checks
andrewrk Sep 14, 2020
2679801
stage2: implement writing archive files
andrewrk Sep 14, 2020
dffdb28
track all TODO comments in BRANCH_TODO file
andrewrk Sep 15, 2020
29d9743
Revert "disable sourcehut freebsd CI checks"
andrewrk Sep 15, 2020
0e94530
stage2: refactor 2 CObject fields to use CSourceFile
andrewrk Sep 15, 2020
6acd903
stage2: support for machine code model CLI
andrewrk Sep 15, 2020
5d8fec3
Merge remote-tracking branch 'origin/master' into stage2-zig-cc
andrewrk Sep 15, 2020
1ad60c4
integrate target features into building C source files
andrewrk Sep 15, 2020
6e9396e
integrate target features into building assembly code
andrewrk Sep 15, 2020
4fa8cc7
stage2: don't depend on windows SDK C++ code when unavailable
andrewrk Sep 15, 2020
1da9e0f
stage2: building glibc libc_nonshared.a CRT file
andrewrk Sep 15, 2020
b2860aa
stage2: add missing import to libc_installation.zig
andrewrk Sep 15, 2020
c803d33
update the zen of zig
andrewrk Sep 15, 2020
f82b183
std: handle sharing violation when deleting a file on windows
andrewrk Sep 15, 2020
a0b43ff
stage2: eliminate one failure path in building c object
andrewrk Sep 15, 2020
99a2fc2
stage2: implement .d file parsing for C objects
andrewrk Sep 16, 2020
16bd0c6
Merge remote-tracking branch 'origin/master' into stage2-zig-cc
andrewrk Sep 16, 2020
da0fde5
stage2: update to new LibCInstallation API
andrewrk Sep 16, 2020
95941c4
stage2: building glibc shared objects
andrewrk Sep 16, 2020
80d8515
ci: disable git autocrlf
andrewrk Sep 16, 2020
a863d89
ci: undo mangled autocrlf files before building
andrewrk Sep 16, 2020
883dcb8
stage2 Cache: use hex instead of base64 for file paths
andrewrk Sep 16, 2020
670260a
stage2: std.log.err is an error not a warning
andrewrk Sep 16, 2020
3256b3c
stage2: glibc shared objects use ok file to detect cache hits
andrewrk Sep 16, 2020
17f094e
stage2: build glibc shared objects using assembly files
andrewrk Sep 16, 2020
17d40ec
stage2: building libunwind.a
andrewrk Sep 17, 2020
07eb2c6
stage2: don't add unused args to assembly compilations
andrewrk Sep 17, 2020
01a7aff
stage2: ask for a higher open fd limit
andrewrk Sep 17, 2020
bc01887
stage2: verify -Wl zig cc behavior
andrewrk Sep 17, 2020
dc47868
delete all stage1 c++ code not directly related to compiling stage2
andrewrk Sep 18, 2020
a9b1802
stage2: implement --show-builtin
andrewrk Sep 18, 2020
dc79651
stage2: add CLI for `zig translate-c`
andrewrk Sep 18, 2020
333b12a
std: start: use std.log instead of stderr
andrewrk Sep 18, 2020
2ef6863
stage2 now supports using stage1 as a backend for compiling zig code
andrewrk Sep 19, 2020
302e156
stage2: make DepTokenizer non-allocating
Vexu Sep 18, 2020
f27bc79
stage2: DepTokenizer add target resolver
Vexu Sep 18, 2020
b759308
stage2: DepTokenizer print errors
Vexu Sep 19, 2020
ced061f
stage2: update uses of DepTokenizer
Vexu Sep 19, 2020
561ed38
ci: drop the cmake patch for linux
andrewrk Sep 19, 2020
bd1465a
cmake: output better message when building self-hosted
andrewrk Sep 21, 2020
b8861a9
stage2: building compiler_rt and libc static archive with stage1
andrewrk Sep 21, 2020
61c54b2
Cache integration for stage1 zig code compilation
andrewrk Sep 22, 2020
b9f61d4
stage1: resolve builtin.zig path when bootstrapping
andrewrk Sep 22, 2020
528832b
rename src-self-hosted/ to src/
andrewrk Sep 22, 2020
ead50ea
stage2: implement `zig run` and `zig test`
andrewrk Sep 22, 2020
afac5d2
fix regressed stage2 test harness
andrewrk Sep 22, 2020
0c70bb4
Merge remote-tracking branch 'origin/master' into stage2-zig-cc
andrewrk Sep 22, 2020
974333f
stage2: fix linking libc trying to depend on itself
andrewrk Sep 22, 2020
877e424
stage2: implement building & linking against libcxx and libcxxabi
andrewrk Sep 22, 2020
bf9a16a
stage2: implement `zig init-lib` and `zig init-exe`
andrewrk Sep 22, 2020
dacd36c
stage2: implement using the global cache dir
andrewrk Sep 22, 2020
250664b
build runner: allow for a bit longer -D options
andrewrk Sep 22, 2020
c2b1cd7
stage2: implement zig build
andrewrk Sep 23, 2020
0638a02
stage2: implement --pkg-begin and --pkg-end CLI args
andrewrk Sep 23, 2020
fc88d36
stage2: link_libc=true on OS's that require it for syscalls
andrewrk Sep 23, 2020
800a4a6
eliminate dependency of libzigcpp.a on libzigstage1.a
andrewrk Sep 23, 2020
15b2bae
fix trying to link libc for static libs and objects
andrewrk Sep 23, 2020
d03fcc7
stage2: implement --main-pkg-path
andrewrk Sep 23, 2020
c0b774f
stage2: support passing extra C flags to C source files
andrewrk Sep 23, 2020
02886a8
stage2: support rpaths
andrewrk Sep 23, 2020
f4dde4d
test_runner: fix missing newline in log
andrewrk Sep 23, 2020
b183b61
stage2: don't build libunwind on OS's that don't need it
andrewrk Sep 23, 2020
90b320d
use ascii range for the --watch REPL prompt
andrewrk Sep 23, 2020
64deb46
stage2: capture LLD stderr into a buffer
andrewrk Sep 23, 2020
b08fd0e
stage2: building musl libc from source
andrewrk Sep 24, 2020
7bbd152
nobody likes my std.process.cleanExit idea
andrewrk Sep 24, 2020
1123c90
stage2: print the test command after it fails
andrewrk Sep 24, 2020
054fafd
stage2: implement @cImport
andrewrk Sep 24, 2020
4f0850f
don't delete C import .c source from zig-cache
andrewrk Sep 25, 2020
67463c4
correct `@cImport` caching to handle "unhit" case
andrewrk Sep 25, 2020
5440ca8
--main-pkg-path properly resolves the relative root src file path
andrewrk Sep 25, 2020
7964341
zig build: update to use new --version flag instead of legacy flags
andrewrk Sep 25, 2020
30dfdfd
fix a round of regressions in this branch
andrewrk Sep 25, 2020
495d18a
std.log: better default for printing logs
andrewrk Sep 25, 2020
812e482
ELF linking supports root src directory to be cwd
andrewrk Sep 25, 2020
670e7d4
add some tracy calls
andrewrk Sep 25, 2020
70d7d7e
stage2: disable lld caching when output dir is owned by user
andrewrk Sep 26, 2020
21b407b
update test case for init-exe with respect to new template
andrewrk Sep 26, 2020
aded86e
std.ArrayHashMap: count and iterator are not deprecated
andrewrk Sep 26, 2020
a337046
stage2: properly handle zig cc used as a preprocessor
andrewrk Sep 26, 2020
6af2990
implement -femit-asm, -femit-docs, -femit-llvm-ir, etc
andrewrk Sep 26, 2020
819625d
fix logic for choosing when dynamic linking is required
andrewrk Sep 26, 2020
56086d1
stage0: update CLI to match stage2
andrewrk Sep 26, 2020
1337f0b
move to stage1-specific cache manifest emission of extra stuff
andrewrk Sep 26, 2020
a23b1b4
stage2: building musl: fix typo in replacing path separators
andrewrk Sep 26, 2020
9b83112
fix _fbsd suffix appended to lld LDM option twice
andrewrk Sep 26, 2020
26f2f9b
stage2: implement -fno-emit-bin
andrewrk Sep 26, 2020
fe4c348
stage2: `zig translate-c` supports --enable-cache
andrewrk Sep 26, 2020
b6556c9
fix another round of regressions in this branch
andrewrk Sep 27, 2020
4b403c7
fix non-ELF linkAsArchive
andrewrk Sep 27, 2020
a9082b4
stage2: add CLI support for --subsystem
andrewrk Sep 27, 2020
91a73a1
stage2: building mingw-w64 and COFF LDD linking
andrewrk Sep 28, 2020
412a2f9
store stage1 flags in a trailing byte in the hash id symlink
andrewrk Sep 28, 2020
ada19c4
stage2: building DLL import lib files
andrewrk Sep 29, 2020
5fed42d
the stage1 backend cache stores inferred link libs
andrewrk Sep 29, 2020
29fd130
std.ChildProcess: bypass libc exit() in fork child error case
andrewrk Sep 29, 2020
ef9582a
`zig test` and `zig run` do not try to run foreign binaries
andrewrk Sep 29, 2020
73167e8
stage2: fix Cache not calling ftruncate in writeManifest
andrewrk Sep 29, 2020
ed06a78
stage2: WASM LLD linking
andrewrk Sep 29, 2020
9d0da16
langref: use general purpose allocator in the wasi example
andrewrk Sep 29, 2020
af6c3a3
mingw: better -D arg for processing def.in files
andrewrk Sep 29, 2020
41f6627
stage2: infer --strip on wasm builds
andrewrk Sep 29, 2020
fa6d150
stage2: MachO LLD Linking
andrewrk Sep 29, 2020
750b00c
Merge remote-tracking branch 'origin/master' into stage2-zig-cc
andrewrk Sep 29, 2020
cfbcb41
stage2: add CLI option for -fstack-report
andrewrk Sep 29, 2020
d3a99c7
add CLI options for darwin frameworks and -ffunction-sections
andrewrk Sep 29, 2020
3efdd7b
don't try to build musl crti.o crtn.o when it's not needed
andrewrk Sep 29, 2020
d8fa8b5
use Allocator.allocSentinel now that the stage1 bug is fixed
andrewrk Sep 29, 2020
7c0ee42
CI: TERM=dumb to source hut
andrewrk Sep 29, 2020
0da7c4b
improve stage2 COFF LLD linking
andrewrk Sep 29, 2020
b811a99
Merge remote-tracking branch 'origin/master' into stage2-zig-cc
andrewrk Sep 29, 2020
f69650a
update wasm to use ".o.wasm" extension for objects
andrewrk Sep 29, 2020
27e008e
fix not passing std.builtin.link_libc to compiler_rt.zig and c.zig
andrewrk Sep 30, 2020
af64fd2
Merge remote-tracking branch 'origin/master' into stage2-zig-cc
andrewrk Sep 30, 2020
1572cd4
different strategy to fix compiler_rt.zig and c.zig
andrewrk Sep 30, 2020
7bffa6f
clean up call to std.fs.rename
andrewrk Sep 30, 2020
839bdfd
link.Coff: skip redundant (and invalid) copy to same path
andrewrk Sep 30, 2020
14d6d07
fix the cli test expected string to support native path separators
andrewrk Sep 30, 2020
870af49
std.zig.binNameAlloc: take into account version for macos dylibs
andrewrk Sep 30, 2020
2a893ef
fix incorrect dylib filename pattern in the previous commit
andrewrk Sep 30, 2020
3249e5d
MachO: add the same workaround for no -r LLD flag support
andrewrk Sep 30, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
149 changes: 69 additions & 80 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ message("Configuring zig version ${ZIG_VERSION}")

set(ZIG_STATIC off CACHE BOOL "Attempt to build a static zig executable (not compatible with glibc)")
set(ZIG_STATIC_LLVM off CACHE BOOL "Prefer linking against static LLVM libraries")
set(ZIG_ENABLE_MEM_PROFILE off CACHE BOOL "Activate memory usage instrumentation")
set(ZIG_PREFER_CLANG_CPP_DYLIB off CACHE BOOL "Try to link against -lclang-cpp")
set(ZIG_WORKAROUND_4799 off CACHE BOOL "workaround for https://github.com/ziglang/zig/issues/4799")
set(ZIG_WORKAROUND_POLLY_SO off CACHE STRING "workaround for https://github.com/ziglang/zig/issues/4799")
Expand All @@ -72,11 +71,6 @@ string(REGEX REPLACE "\\\\" "\\\\\\\\" ZIG_LIBC_INCLUDE_DIR_ESCAPED "${ZIG_LIBC_

option(ZIG_TEST_COVERAGE "Build Zig with test coverage instrumentation" OFF)

# Zig no longer has embedded LLD. This option is kept for package maintainers
# so that they don't have to update their scripts in case we ever re-introduce
# LLD to the tree. This option does nothing.
option(ZIG_FORCE_EXTERNAL_LLD "does nothing" OFF)

set(ZIG_TARGET_TRIPLE "native" CACHE STRING "arch-os-abi to output binaries for")
set(ZIG_TARGET_MCPU "baseline" CACHE STRING "-mcpu parameter to output binaries for")
set(ZIG_EXECUTABLE "" CACHE STRING "(when cross compiling) path to already-built zig binary")
Expand All @@ -101,7 +95,7 @@ if(APPLE AND ZIG_WORKAROUND_4799)
list(APPEND LLVM_LIBRARIES "-Wl,${CMAKE_PREFIX_PATH}/lib/libPolly.a" "-Wl,${CMAKE_PREFIX_PATH}/lib/libPollyPPCG.a" "-Wl,${CMAKE_PREFIX_PATH}/lib/libPollyISL.a")
endif()

set(ZIG_CPP_LIB_DIR "${CMAKE_BINARY_DIR}/zig_cpp")
set(ZIG_CPP_LIB_DIR "${CMAKE_BINARY_DIR}/zigcpp")

# Handle multi-config builds and place each into a common lib. The VS generator
# for example will append a Debug folder by default if not explicitly specified.
Expand Down Expand Up @@ -267,53 +261,45 @@ include_directories("${CMAKE_SOURCE_DIR}/deps/dbg-macro")

find_package(Threads)

# CMake doesn't let us create an empty executable, so we hang on to this one separately.
set(ZIG_MAIN_SRC "${CMAKE_SOURCE_DIR}/src/main.cpp")

# This is our shim which will be replaced by libstage2 written in Zig.
set(ZIG0_SHIM_SRC "${CMAKE_SOURCE_DIR}/src/stage2.cpp")

if(ZIG_ENABLE_MEM_PROFILE)
set(ZIG_SOURCES_MEM_PROFILE "${CMAKE_SOURCE_DIR}/src/mem_profile.cpp")
endif()
# This is our shim which will be replaced by stage1.zig.
set(ZIG0_SOURCES
"${CMAKE_SOURCE_DIR}/src/stage1/zig0.cpp"
)

set(ZIG_SOURCES
"${CMAKE_SOURCE_DIR}/src/analyze.cpp"
"${CMAKE_SOURCE_DIR}/src/ast_render.cpp"
"${CMAKE_SOURCE_DIR}/src/bigfloat.cpp"
"${CMAKE_SOURCE_DIR}/src/bigint.cpp"
"${CMAKE_SOURCE_DIR}/src/buffer.cpp"
"${CMAKE_SOURCE_DIR}/src/cache_hash.cpp"
"${CMAKE_SOURCE_DIR}/src/codegen.cpp"
"${CMAKE_SOURCE_DIR}/src/compiler.cpp"
"${CMAKE_SOURCE_DIR}/src/dump_analysis.cpp"
"${CMAKE_SOURCE_DIR}/src/errmsg.cpp"
"${CMAKE_SOURCE_DIR}/src/error.cpp"
"${CMAKE_SOURCE_DIR}/src/glibc.cpp"
"${CMAKE_SOURCE_DIR}/src/heap.cpp"
"${CMAKE_SOURCE_DIR}/src/ir.cpp"
"${CMAKE_SOURCE_DIR}/src/ir_print.cpp"
"${CMAKE_SOURCE_DIR}/src/link.cpp"
"${CMAKE_SOURCE_DIR}/src/mem.cpp"
"${CMAKE_SOURCE_DIR}/src/os.cpp"
"${CMAKE_SOURCE_DIR}/src/parser.cpp"
"${CMAKE_SOURCE_DIR}/src/range_set.cpp"
"${CMAKE_SOURCE_DIR}/src/target.cpp"
"${CMAKE_SOURCE_DIR}/src/tokenizer.cpp"
"${CMAKE_SOURCE_DIR}/src/util.cpp"
"${CMAKE_SOURCE_DIR}/src/softfloat_ext.cpp"
"${ZIG_SOURCES_MEM_PROFILE}"
set(STAGE1_SOURCES
"${CMAKE_SOURCE_DIR}/src/stage1/analyze.cpp"
"${CMAKE_SOURCE_DIR}/src/stage1/ast_render.cpp"
"${CMAKE_SOURCE_DIR}/src/stage1/bigfloat.cpp"
"${CMAKE_SOURCE_DIR}/src/stage1/bigint.cpp"
"${CMAKE_SOURCE_DIR}/src/stage1/buffer.cpp"
"${CMAKE_SOURCE_DIR}/src/stage1/codegen.cpp"
"${CMAKE_SOURCE_DIR}/src/stage1/dump_analysis.cpp"
"${CMAKE_SOURCE_DIR}/src/stage1/errmsg.cpp"
"${CMAKE_SOURCE_DIR}/src/stage1/error.cpp"
"${CMAKE_SOURCE_DIR}/src/stage1/heap.cpp"
"${CMAKE_SOURCE_DIR}/src/stage1/ir.cpp"
"${CMAKE_SOURCE_DIR}/src/stage1/ir_print.cpp"
"${CMAKE_SOURCE_DIR}/src/stage1/mem.cpp"
"${CMAKE_SOURCE_DIR}/src/stage1/os.cpp"
"${CMAKE_SOURCE_DIR}/src/stage1/parser.cpp"
"${CMAKE_SOURCE_DIR}/src/stage1/range_set.cpp"
"${CMAKE_SOURCE_DIR}/src/stage1/stage1.cpp"
"${CMAKE_SOURCE_DIR}/src/stage1/target.cpp"
"${CMAKE_SOURCE_DIR}/src/stage1/tokenizer.cpp"
"${CMAKE_SOURCE_DIR}/src/stage1/util.cpp"
"${CMAKE_SOURCE_DIR}/src/stage1/softfloat_ext.cpp"
)
set(OPTIMIZED_C_SOURCES
"${CMAKE_SOURCE_DIR}/src/blake2b.c"
"${CMAKE_SOURCE_DIR}/src/parse_f128.c"
"${CMAKE_SOURCE_DIR}/src/stage1/parse_f128.c"
)
set(ZIG_CPP_SOURCES
# These are planned to stay even when we are self-hosted.
"${CMAKE_SOURCE_DIR}/src/zig_llvm.cpp"
"${CMAKE_SOURCE_DIR}/src/zig_clang.cpp"
"${CMAKE_SOURCE_DIR}/src/zig_clang_driver.cpp"
"${CMAKE_SOURCE_DIR}/src/zig_clang_cc1_main.cpp"
"${CMAKE_SOURCE_DIR}/src/zig_clang_cc1as_main.cpp"
# https://github.com/ziglang/zig/issues/6363
"${CMAKE_SOURCE_DIR}/src/windows_sdk.cpp"
)

Expand All @@ -334,7 +320,7 @@ set(ZIG_STD_DEST "${ZIG_LIB_DIR}/std")
set(ZIG_CONFIG_H_OUT "${CMAKE_BINARY_DIR}/config.h")
set(ZIG_CONFIG_ZIG_OUT "${CMAKE_BINARY_DIR}/config.zig")
configure_file (
"${CMAKE_SOURCE_DIR}/src/config.h.in"
"${CMAKE_SOURCE_DIR}/src/stage1/config.h.in"
"${ZIG_CONFIG_H_OUT}"
)
configure_file (
Expand All @@ -346,6 +332,7 @@ include_directories(
${CMAKE_SOURCE_DIR}
${CMAKE_BINARY_DIR}
"${CMAKE_SOURCE_DIR}/src"
"${CMAKE_SOURCE_DIR}/src/stage1"
)

# These have to go before the -Wno- flags
Expand Down Expand Up @@ -411,114 +398,116 @@ if(ZIG_TEST_COVERAGE)
set(EXE_LDFLAGS "${EXE_LDFLAGS} -fprofile-arcs -ftest-coverage")
endif()

add_library(zig_cpp STATIC ${ZIG_CPP_SOURCES})
set_target_properties(zig_cpp PROPERTIES
add_library(zigcpp STATIC ${ZIG_CPP_SOURCES})
set_target_properties(zigcpp PROPERTIES
COMPILE_FLAGS ${EXE_CFLAGS}
)

target_link_libraries(zig_cpp LINK_PUBLIC
target_link_libraries(zigcpp LINK_PUBLIC
${CLANG_LIBRARIES}
${LLD_LIBRARIES}
${LLVM_LIBRARIES}
${CMAKE_THREAD_LIBS_INIT}
)
if(ZIG_WORKAROUND_POLLY_SO)
target_link_libraries(zig_cpp LINK_PUBLIC "-Wl,${ZIG_WORKAROUND_POLLY_SO}")
target_link_libraries(zigcpp LINK_PUBLIC "-Wl,${ZIG_WORKAROUND_POLLY_SO}")
endif()

add_library(opt_c_util STATIC ${OPTIMIZED_C_SOURCES})
set_target_properties(opt_c_util PROPERTIES
COMPILE_FLAGS "${OPTIMIZED_C_FLAGS}"
)

add_library(zigcompiler STATIC ${ZIG_SOURCES})
set_target_properties(zigcompiler PROPERTIES
add_library(zigstage1 STATIC ${STAGE1_SOURCES})
set_target_properties(zigstage1 PROPERTIES
COMPILE_FLAGS ${EXE_CFLAGS}
LINK_FLAGS ${EXE_LDFLAGS}
)
target_link_libraries(zigcompiler LINK_PUBLIC
zig_cpp
target_link_libraries(zigstage1 LINK_PUBLIC
opt_c_util
${SOFTFLOAT_LIBRARIES}
${CMAKE_THREAD_LIBS_INIT}
zigcpp
)
if(NOT MSVC)
target_link_libraries(zigcompiler LINK_PUBLIC ${LIBXML2})
target_link_libraries(zigstage1 LINK_PUBLIC ${LIBXML2})
endif()

if(ZIG_DIA_GUIDS_LIB)
target_link_libraries(zigcompiler LINK_PUBLIC ${ZIG_DIA_GUIDS_LIB})
target_link_libraries(zigstage1 LINK_PUBLIC ${ZIG_DIA_GUIDS_LIB})
endif()

if(MSVC OR MINGW)
target_link_libraries(zigcompiler LINK_PUBLIC version)
target_link_libraries(zigstage1 LINK_PUBLIC version)
endif()

add_executable(zig0 "${ZIG_MAIN_SRC}" "${ZIG0_SHIM_SRC}")
add_executable(zig0 ${ZIG0_SOURCES})
set_target_properties(zig0 PROPERTIES
COMPILE_FLAGS ${EXE_CFLAGS}
LINK_FLAGS ${EXE_LDFLAGS}
)
target_link_libraries(zig0 zigcompiler)
target_link_libraries(zig0 zigstage1)

if(MSVC)
set(LIBSTAGE2 "${CMAKE_BINARY_DIR}/zigstage2.lib")
set(ZIG1_OBJECT "${CMAKE_BINARY_DIR}/zig1.obj")
else()
set(LIBSTAGE2 "${CMAKE_BINARY_DIR}/libzigstage2.a")
set(ZIG1_OBJECT "${CMAKE_BINARY_DIR}/zig1.o")
endif()
if("${CMAKE_BUILD_TYPE}" STREQUAL "Debug")
set(LIBSTAGE2_RELEASE_ARG "")
set(ZIG1_RELEASE_ARG "")
else()
set(LIBSTAGE2_RELEASE_ARG --release-fast --strip)
set(ZIG1_RELEASE_ARG -OReleaseFast --strip)
endif()

set(BUILD_LIBSTAGE2_ARGS "build-lib"
"src-self-hosted/stage2.zig"
set(BUILD_ZIG1_ARGS
"src/stage1.zig"
-target "${ZIG_TARGET_TRIPLE}"
"-mcpu=${ZIG_TARGET_MCPU}"
--name zigstage2
--name zig1
--override-lib-dir "${CMAKE_SOURCE_DIR}/lib"
--cache on
--output-dir "${CMAKE_BINARY_DIR}"
${LIBSTAGE2_RELEASE_ARG}
--bundle-compiler-rt
-fPIC
"-femit-bin=${ZIG1_OBJECT}"
"${ZIG1_RELEASE_ARG}"
-lc
--pkg-begin build_options "${ZIG_CONFIG_ZIG_OUT}"
--pkg-end
--pkg-begin compiler_rt "${CMAKE_SOURCE_DIR}/lib/std/special/compiler_rt.zig"
--pkg-end
)

if("${ZIG_TARGET_TRIPLE}" STREQUAL "native")
add_custom_target(zig_build_libstage2 ALL
COMMAND zig0 ${BUILD_LIBSTAGE2_ARGS}
add_custom_target(zig_build_zig1 ALL
COMMAND zig0 ${BUILD_ZIG1_ARGS}
DEPENDS zig0
BYPRODUCTS "${LIBSTAGE2}"
BYPRODUCTS "${ZIG1_OBJECT}"
COMMENT STATUS "Building self-hosted component ${ZIG1_OBJECT}"
WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}"
)
set(ZIG_EXECUTABLE "${zig_BINARY_DIR}/zig")
if (WIN32)
set(ZIG_EXECUTABLE "${ZIG_EXECUTABLE}.exe")
endif()
else()
add_custom_target(zig_build_libstage2 ALL
COMMAND "${ZIG_EXECUTABLE}" ${BUILD_LIBSTAGE2_ARGS}
BYPRODUCTS "${LIBSTAGE2}"
add_custom_target(zig_build_zig1 ALL
COMMAND "${ZIG_EXECUTABLE}" ${BUILD_ZIG1_ARGS}
BYPRODUCTS "${ZIG1_OBJECT}"
COMMENT STATUS "Building self-hosted component ${ZIG1_OBJECT}"
WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}"
)
endif()

add_executable(zig "${ZIG_MAIN_SRC}")
# cmake won't let us configure an executable without C sources.
add_executable(zig "${CMAKE_SOURCE_DIR}/src/stage1/empty.cpp")

set_target_properties(zig PROPERTIES
COMPILE_FLAGS ${EXE_CFLAGS}
LINK_FLAGS ${EXE_LDFLAGS}
)
target_link_libraries(zig zigcompiler "${LIBSTAGE2}")
target_link_libraries(zig "${ZIG1_OBJECT}" zigstage1)
if(MSVC)
target_link_libraries(zig ntdll.lib)
elseif(MINGW)
target_link_libraries(zig ntdll)
endif()
add_dependencies(zig zig_build_libstage2)
add_dependencies(zig zig_build_zig1)

install(TARGETS zig DESTINATION bin)

Expand Down
37 changes: 37 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ Note that you can

### Stage 1: Build Zig from C++ Source Code

This step must be repeated when you make changes to any of the C++ source code.

#### Dependencies

##### POSIX
Expand Down Expand Up @@ -82,6 +84,41 @@ in which case you can try `-DZIG_WORKAROUND_6087=ON`.

See https://github.com/ziglang/zig/wiki/Building-Zig-on-Windows

### Stage 2: Build Self-Hosted Zig from Zig Source Code

Now we use the stage1 binary:

```
zig build --prefix $(pwd)/stage2 -Denable-llvm
```

This produces `stage2/bin/zig` which can be used for testing and development.
Once it is feature complete, it will be used to build stage 3 - the final compiler
binary.

### Stage 3: Rebuild Self-Hosted Zig Using the Self-Hosted Compiler

*Note: Stage 2 compiler is not yet able to build Stage 3. Building Stage 3 is
not yet supported.*

Once the self-hosted compiler can build itself, this will be the actual
compiler binary that we will install to the system. Until then, users should
use stage 1.

#### Debug / Development Build

```
stage2/bin/zig build
```

This produces `zig-cache/bin/zig`.

#### Release / Install Build

```
stage2/bin/zig build install -Drelease
```

## License

The ultimate goal of the Zig project is to serve users. As a first-order
Expand Down
Loading