diff --git a/CMakeLists.txt b/CMakeLists.txt index cbd1317bdb..ea367c00a2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -15,7 +15,7 @@ set(CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake ${CMAKE_MODULE_PATH}) set(ZIG_VERSION_MAJOR 0) -set(ZIG_VERSION_MINOR 3) +set(ZIG_VERSION_MINOR 4) set(ZIG_VERSION_PATCH 0) set(ZIG_VERSION "${ZIG_VERSION_MAJOR}.${ZIG_VERSION_MINOR}.${ZIG_VERSION_PATCH}") @@ -50,10 +50,6 @@ option(ZIG_FORCE_EXTERNAL_LLD "If your system has the LLD patches use it instead find_package(llvm) find_package(clang) -if(MINGW) - find_package(z3) -endif() - if(APPLE AND ZIG_STATIC) list(REMOVE_ITEM LLVM_LIBRARIES "-lz") find_library(ZLIB NAMES z zlib libz) @@ -62,6 +58,16 @@ endif() set(ZIG_CPP_LIB_DIR "${CMAKE_BINARY_DIR}/zig_cpp") +# 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. +set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${ZIG_CPP_LIB_DIR}) +set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${ZIG_CPP_LIB_DIR}) +foreach(CONFIG_TYPE ${CMAKE_CONFIGURATION_TYPES}) + string(TOUPPER ${CONFIG_TYPE} CONFIG_TYPE) + set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY_${CONFIG_TYPE} ${ZIG_CPP_LIB_DIR}) + set(CMAKE_LIBRARY_OUTPUT_DIRECTORY_${CONFIG_TYPE} ${ZIG_CPP_LIB_DIR}) +endforeach(CONFIG_TYPE CMAKE_CONFIGURATION_TYPES) + if(ZIG_FORCE_EXTERNAL_LLD) find_package(lld) include_directories(${LLVM_INCLUDE_DIRS}) @@ -196,7 +202,7 @@ else() if(MSVC) set(ZIG_LLD_COMPILE_FLAGS "-std=c++11 -D_CRT_SECURE_NO_WARNINGS /w") else() - set(ZIG_LLD_COMPILE_FLAGS "-std=c++11 -fno-exceptions -fno-rtti -Wno-comment") + set(ZIG_LLD_COMPILE_FLAGS "-std=c++11 -fvisibility-inlines-hidden -fno-exceptions -fno-rtti -Wno-comment") if(MINGW) set(ZIG_LLD_COMPILE_FLAGS "${ZIG_LLD_COMPILE_FLAGS} -D__STDC_FORMAT_MACROS -D__USE_MINGW_ANSI_STDIO -Wno-pedantic-ms-format") endif() @@ -257,7 +263,6 @@ else() embedded_lld_wasm embedded_lld_lib ) - install(TARGETS embedded_lld_elf embedded_lld_coff embedded_lld_mingw embedded_lld_wasm embedded_lld_lib DESTINATION "${ZIG_CPP_LIB_DIR}") endif() # No patches have been applied to SoftFloat-3e @@ -407,6 +412,12 @@ set(SOFTFLOAT_LIBRARIES embedded_softfloat) 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 libuserland written in Zig. +set(ZIG0_SHIM_SRC "${CMAKE_SOURCE_DIR}/src/userland.cpp") + set(ZIG_SOURCES "${CMAKE_SOURCE_DIR}/src/analyze.cpp" "${CMAKE_SOURCE_DIR}/src/ast_render.cpp" @@ -423,7 +434,6 @@ set(ZIG_SOURCES "${CMAKE_SOURCE_DIR}/src/ir_print.cpp" "${CMAKE_SOURCE_DIR}/src/libc_installation.cpp" "${CMAKE_SOURCE_DIR}/src/link.cpp" - "${CMAKE_SOURCE_DIR}/src/main.cpp" "${CMAKE_SOURCE_DIR}/src/os.cpp" "${CMAKE_SOURCE_DIR}/src/parser.cpp" "${CMAKE_SOURCE_DIR}/src/range_set.cpp" @@ -477,6 +487,7 @@ set(ZIG_STD_FILES "crypto/x25519.zig" "cstr.zig" "debug.zig" + "debug/leb128.zig" "debug/failing_allocator.zig" "dwarf.zig" "dynamic_library.zig" @@ -507,6 +518,7 @@ set(ZIG_STD_FILES "heap.zig" "io.zig" "io/seekable_stream.zig" + "io/c_out_stream.zig" "json.zig" "lazy_init.zig" "linked_list.zig" @@ -521,6 +533,7 @@ set(ZIG_STD_FILES "math/atanh.zig" "math/big.zig" "math/big/int.zig" + "math/big/rational.zig" "math/cbrt.zig" "math/ceil.zig" "math/complex.zig" @@ -599,6 +612,7 @@ set(ZIG_STD_FILES "os/linux.zig" "os/linux/arm64.zig" "os/linux/errno.zig" + "os/linux/tls.zig" "os/linux/vdso.zig" "os/linux/x86_64.zig" "os/netbsd.zig" @@ -606,6 +620,8 @@ set(ZIG_STD_FILES "os/path.zig" "os/time.zig" "os/uefi.zig" + "os/wasi.zig" + "os/wasi/core.zig" "os/windows.zig" "os/windows/advapi32.zig" "os/windows/error.zig" @@ -615,6 +631,7 @@ set(ZIG_STD_FILES "os/windows/shell32.zig" "os/windows/util.zig" "os/zen.zig" + "packed_int_array.zig" "pdb.zig" "priority_queue.zig" "rand.zig" @@ -628,10 +645,15 @@ set(ZIG_STD_FILES "special/build_runner.zig" "special/builtin.zig" "special/compiler_rt.zig" + "special/compiler_rt/stack_probe.zig" + "special/compiler_rt/arm/aeabi_fcmp.zig" + "special/compiler_rt/arm/aeabi_dcmp.zig" "special/compiler_rt/addXf3.zig" "special/compiler_rt/aulldiv.zig" "special/compiler_rt/aullrem.zig" "special/compiler_rt/comparetf2.zig" + "special/compiler_rt/comparedf2.zig" + "special/compiler_rt/comparesf2.zig" "special/compiler_rt/divsf3.zig" "special/compiler_rt/divdf3.zig" "special/compiler_rt/divti3.zig" @@ -656,9 +678,13 @@ set(ZIG_STD_FILES "special/compiler_rt/fixunstfdi.zig" "special/compiler_rt/fixunstfsi.zig" "special/compiler_rt/fixunstfti.zig" + "special/compiler_rt/floatdidf.zig" + "special/compiler_rt/floatsiXf.zig" + "special/compiler_rt/floatunsidf.zig" "special/compiler_rt/floattidf.zig" "special/compiler_rt/floattisf.zig" "special/compiler_rt/floattitf.zig" + "special/compiler_rt/floatundidf.zig" "special/compiler_rt/floatunditf.zig" "special/compiler_rt/floatunsitf.zig" "special/compiler_rt/floatuntidf.zig" @@ -667,7 +693,11 @@ set(ZIG_STD_FILES "special/compiler_rt/modti3.zig" "special/compiler_rt/mulXf3.zig" "special/compiler_rt/muloti4.zig" + "special/compiler_rt/mulodi4.zig" "special/compiler_rt/multi3.zig" + "special/compiler_rt/ashlti3.zig" + "special/compiler_rt/ashrti3.zig" + "special/compiler_rt/lshrti3.zig" "special/compiler_rt/negXf2.zig" "special/compiler_rt/popcountdi2.zig" "special/compiler_rt/truncXfYf2.zig" @@ -676,7 +706,6 @@ set(ZIG_STD_FILES "special/compiler_rt/udivmodti4.zig" "special/compiler_rt/udivti3.zig" "special/compiler_rt/umodti3.zig" - "special/fmt_runner.zig" "special/init-exe/build.zig" "special/init-exe/src/main.zig" "special/init-lib/build.zig" @@ -6604,7 +6633,7 @@ endif() if(MSVC) set(EXE_CFLAGS "${EXE_CFLAGS}") else() - set(EXE_CFLAGS "${EXE_CFLAGS} -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS -D_GNU_SOURCE -fno-exceptions -fno-rtti -Werror=strict-prototypes -Werror=old-style-definition -Werror=type-limits -Wno-missing-braces") + set(EXE_CFLAGS "${EXE_CFLAGS} -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS -D_GNU_SOURCE -fvisibility-inlines-hidden -fno-exceptions -fno-rtti -Werror=strict-prototypes -Werror=old-style-definition -Werror=type-limits -Wno-missing-braces") if(MINGW) set(EXE_CFLAGS "${EXE_CFLAGS} -D__USE_MINGW_ANSI_STDIO -Wno-pedantic-ms-format") endif() @@ -6639,13 +6668,12 @@ set_target_properties(opt_c_util PROPERTIES COMPILE_FLAGS "${OPTIMIZED_C_FLAGS}" ) -add_executable(zig ${ZIG_SOURCES}) -set_target_properties(zig PROPERTIES +add_library(compiler STATIC ${ZIG_SOURCES}) +set_target_properties(compiler PROPERTIES COMPILE_FLAGS ${EXE_CFLAGS} LINK_FLAGS ${EXE_LDFLAGS} ) - -target_link_libraries(zig LINK_PUBLIC +target_link_libraries(compiler LINK_PUBLIC zig_cpp opt_c_util ${SOFTFLOAT_LIBRARIES} @@ -6654,24 +6682,63 @@ target_link_libraries(zig LINK_PUBLIC ${LLVM_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} ) - if(NOT MSVC) - target_link_libraries(zig LINK_PUBLIC ${LIBXML2}) + target_link_libraries(compiler LINK_PUBLIC ${LIBXML2}) endif() if(MINGW) - target_link_libraries(zig LINK_PUBLIC ${Z3_LIBRARIES}) + find_library(Z3_LIBRARIES NAMES z3 z3.dll) + target_link_libraries(compiler LINK_PUBLIC ${Z3_LIBRARIES}) endif() if(ZIG_DIA_GUIDS_LIB) - target_link_libraries(zig LINK_PUBLIC ${ZIG_DIA_GUIDS_LIB}) + target_link_libraries(compiler LINK_PUBLIC ${ZIG_DIA_GUIDS_LIB}) endif() if(MSVC OR MINGW) - target_link_libraries(zig LINK_PUBLIC version) + target_link_libraries(compiler LINK_PUBLIC version) endif() + +add_executable(zig0 "${ZIG_MAIN_SRC}" "${ZIG0_SHIM_SRC}") +set_target_properties(zig0 PROPERTIES + COMPILE_FLAGS ${EXE_CFLAGS} + LINK_FLAGS ${EXE_LDFLAGS} +) +target_link_libraries(zig0 compiler) + +if(WIN32) + set(LIBUSERLAND "${CMAKE_BINARY_DIR}/userland.lib") +elseif(APPLE) + set(LIBUSERLAND "${CMAKE_BINARY_DIR}/userland.o") +else() + set(LIBUSERLAND "${CMAKE_BINARY_DIR}/libuserland.a") +endif() +add_custom_command( + OUTPUT "${LIBUSERLAND}" + COMMAND zig0 ARGS build + --override-std-dir std + --override-lib-dir "${CMAKE_SOURCE_DIR}" + libuserland + "-Doutput-dir=${CMAKE_BINARY_DIR}" + WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}" + DEPENDS + "${CMAKE_SOURCE_DIR}/src-self-hosted/stage1.zig" + "${CMAKE_SOURCE_DIR}/src-self-hosted/translate_c.zig" + "${CMAKE_SOURCE_DIR}/build.zig" +) +add_custom_target(userland_target DEPENDS "${LIBUSERLAND}") +add_executable(zig "${ZIG_MAIN_SRC}") +if(MINGW) + set(EXE_LDFLAGS "${EXE_LDFLAGS} -fstack-protector") +endif() +set_target_properties(zig PROPERTIES + COMPILE_FLAGS ${EXE_CFLAGS} + LINK_FLAGS ${EXE_LDFLAGS} +) +target_link_libraries(zig compiler "${LIBUSERLAND}") +add_dependencies(zig userland_target) install(TARGETS zig DESTINATION bin) -install(TARGETS zig_cpp DESTINATION "${ZIG_CPP_LIB_DIR}") + foreach(file ${ZIG_C_HEADER_FILES}) get_filename_component(file_dir "${C_HEADERS_DEST}/${file}" DIRECTORY) @@ -6697,7 +6764,3 @@ foreach(file ${ZIG_LIBCXX_FILES}) get_filename_component(file_dir "${LIBCXX_FILES_DEST}/${file}" DIRECTORY) install(FILES "${CMAKE_SOURCE_DIR}/libcxx/${file}" DESTINATION "${file_dir}") endforeach() - -install(FILES "${CMAKE_SOURCE_DIR}/src-self-hosted/arg.zig" DESTINATION "${ZIG_STD_DEST}/special/fmt/") -install(FILES "${CMAKE_SOURCE_DIR}/src-self-hosted/main.zig" DESTINATION "${ZIG_STD_DEST}/special/fmt/") -install(FILES "${CMAKE_SOURCE_DIR}/src-self-hosted/errmsg.zig" DESTINATION "${ZIG_STD_DEST}/special/fmt/") diff --git a/README.md b/README.md index 77ed6d22c6..4ca79232d8 100644 --- a/README.md +++ b/README.md @@ -1,140 +1,15 @@ ![ZIG](https://ziglang.org/zig-logo.svg) -A programming language designed for robustness, optimality, and -clarity. +Zig is an open-source programming language designed for **robustness**, +**optimality**, and **maintainability**. -[Download & Documentation](https://ziglang.org/download/) +## Resources -## Feature Highlights + * [Introduction](https://ziglang.org/#Introduction) + * [Download & Documentation](https://ziglang.org/download) + * [Community](https://github.com/ziglang/zig/wiki/Community) - * Small, simple language. Focus on debugging your application rather than - debugging knowledge of your programming language. - * Ships with a build system that obviates the need for a configure script - or a makefile. In fact, existing C and C++ projects may choose to depend on - Zig instead of e.g. cmake. - * A fresh take on error handling which makes writing correct code easier than - writing buggy code. - * Debug mode optimizes for fast compilation time and crashing with a stack trace - when undefined behavior *would* happen. - * ReleaseFast mode produces heavily optimized code. What other projects call - "Link Time Optimization" Zig does automatically. - * Compatible with C libraries with no wrapper necessary. Directly include - C .h files and get access to the functions and symbols therein. - * Provides standard library which competes with the C standard library and is - always compiled against statically in source form. Zig binaries do not - depend on libc unless explicitly linked. - * Optional type instead of null pointers. - * Safe unions, tagged unions, and C ABI compatible unions. - * Generics so that one can write efficient data structures that work for any - data type. - * No header files required. Top level declarations are entirely - order-independent. - * Compile-time code execution. Compile-time reflection. - * Partial compile-time function evaluation which eliminates the need for - a preprocessor or macros. - * The binaries produced by Zig have complete debugging information so you can, - for example, use GDB, MSVC, or LLDB to debug your software. - * Built-in unit tests with `zig test`. - * Friendly toward package maintainers. Reproducible build, bootstrapping - process carefully documented. Issues filed by package maintainers are - considered especially important. - * Cross-compiling is a primary use case. - * In addition to creating executables, creating a C library is a primary use - case. You can export an auto-generated .h file. - -### Supported Targets - -#### Tier 1 Support - - * Not only can Zig generate machine code for these targets, but the standard - library cross-platform abstractions have implementations for these targets. - Thus it is practical to write a pure Zig application with no dependency on - libc. - * The CI server automatically tests these targets on every commit to master - branch, and updates ziglang.org/download with links to pre-built binaries. - * These targets have debug info capabilities and therefore produce stack - traces on failed assertions. - * ([coming soon](https://github.com/ziglang/zig/issues/514)) libc is available - for this target even when cross compiling. - -#### Tier 2 Support - - * There may be some standard library implementations, but many abstractions - will give an "Unsupported OS" compile error. One can link with libc or other - libraries to fill in the gaps in the standard library. - * These targets are known to work, but are not automatically tested, so there - are occasional regressions. - * Some tests may be disabled for these targets as we work toward Tier 1 - support. - -#### Tier 3 Support - - * The standard library has little to no knowledge of the existence of this - target. - * Because Zig is based on LLVM, it has the capability to build for these - targets, and LLVM has the target enabled by default. - * These targets are not frequently tested; one will likely need to contribute - to Zig in order to build for these targets. - * The Zig compiler might need to be updated with a few things such as - - what sizes are the C integer types - - C ABI calling convention for this target - - bootstrap code and default panic handler - * `zig targets` is guaranteed to include this target. - -#### Tier 4 Support - - * Support for these targets is entirely experimental. - * LLVM may have the target as an experimental target, which means that you - need to use Zig-provided binaries for the target to be available, or - build LLVM from source with special configure flags. `zig targets` will - display the target if it is available. - * This target may be considered deprecated by an official party, - [such as macosx/i386](https://support.apple.com/en-us/HT208436) in which - case this target will remain forever stuck in Tier 4. - * This target may only support `--emit asm` and cannot emit object files. - -#### Support Table - -| | freestanding | linux | macosx | windows | freebsd | netbsd | UEFI | other | -|-------------|--------------|--------|--------|---------|---------|------- | -------|--------| -|x86_64 | Tier 2 | Tier 1 | Tier 1 | Tier 1 | Tier 2 | Tier 2 | Tier 2 | Tier 3 | -|i386 | Tier 2 | Tier 2 | Tier 4 | Tier 2 | Tier 3 | Tier 3 | Tier 3 | Tier 3 | -|arm | Tier 2 | Tier 3 | Tier 3 | Tier 3 | Tier 3 | Tier 3 | Tier 3 | Tier 3 | -|arm64 | Tier 2 | Tier 2 | Tier 3 | Tier 3 | Tier 3 | Tier 3 | Tier 3 | Tier 3 | -|bpf | Tier 3 | Tier 3 | N/A | N/A | Tier 3 | Tier 3 | N/A | Tier 3 | -|hexagon | Tier 3 | Tier 3 | N/A | N/A | Tier 3 | Tier 3 | N/A | Tier 3 | -|mips | Tier 3 | Tier 3 | N/A | N/A | Tier 3 | Tier 3 | N/A | Tier 3 | -|powerpc | Tier 3 | Tier 3 | N/A | N/A | Tier 3 | Tier 3 | N/A | Tier 3 | -|amdgcn | Tier 3 | Tier 3 | N/A | N/A | Tier 3 | Tier 3 | N/A | Tier 3 | -|sparc | Tier 3 | Tier 3 | N/A | N/A | Tier 3 | Tier 3 | N/A | Tier 3 | -|s390x | Tier 3 | Tier 3 | N/A | N/A | Tier 3 | Tier 3 | N/A | Tier 3 | -|lanai | Tier 3 | Tier 3 | N/A | N/A | Tier 3 | Tier 3 | N/A | Tier 3 | -|wasm32 | Tier 3 | N/A | N/A | N/A | N/A | N/A | N/A | N/A | -|wasm64 | Tier 3 | N/A | N/A | N/A | N/A | N/A | N/A | N/A | -|avr | Tier 4 | Tier 4 | N/A | N/A | Tier 4 | Tier 4 | N/A | Tier 4 | -|riscv32 | Tier 4 | Tier 4 | N/A | N/A | Tier 4 | Tier 4 | Tier 4 | Tier 4 | -|riscv64 | Tier 4 | Tier 4 | N/A | N/A | Tier 4 | Tier 4 | Tier 4 | Tier 4 | -|xcore | Tier 4 | Tier 4 | N/A | N/A | Tier 4 | Tier 4 | N/A | Tier 4 | -|nvptx | Tier 4 | Tier 4 | N/A | N/A | Tier 4 | Tier 4 | N/A | Tier 4 | -|msp430 | Tier 4 | Tier 4 | N/A | N/A | Tier 4 | Tier 4 | N/A | Tier 4 | -|r600 | Tier 4 | Tier 4 | N/A | N/A | Tier 4 | Tier 4 | N/A | Tier 4 | -|arc | Tier 4 | Tier 4 | N/A | N/A | Tier 4 | Tier 4 | N/A | Tier 4 | -|tce | Tier 4 | Tier 4 | N/A | N/A | Tier 4 | Tier 4 | N/A | Tier 4 | -|le | Tier 4 | Tier 4 | N/A | N/A | Tier 4 | Tier 4 | N/A | Tier 4 | -|amdil | Tier 4 | Tier 4 | N/A | N/A | Tier 4 | Tier 4 | N/A | Tier 4 | -|hsail | Tier 4 | Tier 4 | N/A | N/A | Tier 4 | Tier 4 | N/A | Tier 4 | -|spir | Tier 4 | Tier 4 | N/A | N/A | Tier 4 | Tier 4 | N/A | Tier 4 | -|kalimba | Tier 4 | Tier 4 | N/A | N/A | Tier 4 | Tier 4 | N/A | Tier 4 | -|shave | Tier 4 | Tier 4 | N/A | N/A | Tier 4 | Tier 4 | N/A | Tier 4 | -|renderscript | Tier 4 | Tier 4 | N/A | N/A | Tier 4 | Tier 4 | N/A | Tier 4 | - -## Community - - * IRC: `#zig` on Freenode ([Channel Logs](https://irclog.whitequark.org/zig/)). - * Reddit: [/r/zig](https://www.reddit.com/r/zig) - * Email list: [~andrewrk/ziglang@lists.sr.ht](https://lists.sr.ht/%7Eandrewrk/ziglang) - -## Building +## Building from Source [![Build Status](https://dev.azure.com/ziglang/zig/_apis/build/status/ziglang.zig?branchName=master)](https://dev.azure.com/ziglang/zig/_build/latest?definitionId=1&branchName=master) @@ -150,12 +25,14 @@ Note that you can * cmake >= 2.8.5 * gcc >= 5.0.0 or clang >= 3.6.0 * LLVM, Clang, LLD development libraries == 8.x, compiled with the same gcc or clang version above + - Use the system package manager, or [build from source](https://github.com/ziglang/zig/wiki/How-to-build-LLVM,-libclang,-and-liblld-from-source#posix). ##### Windows * cmake >= 2.8.5 * Microsoft Visual Studio 2017 (version 15.8) * LLVM, Clang, LLD development libraries == 8.x, compiled with the same MSVC version above + - Use the [pre-built binaries](https://github.com/ziglang/zig/wiki/How-to-build-LLVM,-libclang,-and-liblld-from-source#pre-built-binaries) or [build from source](https://github.com/ziglang/zig/wiki/How-to-build-LLVM,-libclang,-and-liblld-from-source#windows). #### Instructions @@ -165,9 +42,7 @@ Note that you can mkdir build cd build cmake .. -make make install -bin/zig build --build-file ../build.zig test ``` ##### MacOS @@ -179,7 +54,6 @@ mkdir build cd build cmake .. -DCMAKE_PREFIX_PATH=/usr/local/Cellar/llvm/8.0.0 make install -bin/zig build --build-file ../build.zig test ``` ##### Windows @@ -222,3 +96,95 @@ use stage 1. ``` ./stage2/bin/zig build --build-file ../build.zig install -Drelease-fast ``` + +## Contributing + +### Start a Project Using Zig + +One of the best ways you can contribute to Zig is to start using it for a +personal project. Here are some great examples: + + * [Oxid](https://github.com/dbandstra/oxid) - arcade style game + * [TM35-Metronome](https://github.com/TM35-Metronome) - tools for modifying and randomizing Pokémon games + * [trOS](https://github.com/sjdh02/trOS) - tiny aarch64 baremetal OS thingy + +Without fail, these projects lead to discovering bugs and helping flesh out use +cases, which lead to further design iterations of Zig. Importantly, each issue +found this way comes with real world motivations, so it is easy to explain +your reasoning behind proposals and feature requests. + +Ideally, such a project will help you to learn new skills and add something +to your personal portfolio at the same time. + +### Spread the Word + +Another way to contribute is to write about Zig, or speak about Zig at a +conference, or do either of those things for your project which uses Zig. +Here are some examples: + + * [Iterative Replacement of C with Zig](http://tiehuis.github.io/blog/zig1.html) + * [The Right Tool for the Right Job: Redis Modules & Zig](https://www.youtube.com/watch?v=eCHM8-_poZY) + +Zig is a brand new language, with no advertising budget. Word of mouth is the +only way people find out about the project, and the more people hear about it, +the more people will use it, and the better chance we have to take over the +world. + +### Finding Contributor Friendly Issues + +Please note that issues labeled +[Proposal](https://github.com/ziglang/zig/issues?q=is%3Aissue+is%3Aopen+label%3Aproposal) +but do not also have the +[Accepted](https://github.com/ziglang/zig/issues?q=is%3Aissue+is%3Aopen+label%3Aaccepted) +label are still under consideration, and efforts to implement such a proposal +have a high risk of being wasted. If you are interested in a proposal which is +still under consideration, please express your interest in the issue tracker, +providing extra insights and considerations that others have not yet expressed. +The most highly regarded argument in such a discussion is a real world use case. + +The issue label +[Contributor Friendly](https://github.com/ziglang/zig/issues?q=is%3Aissue+is%3Aopen+label%3A%22contributor+friendly%22) +exists to help contributors find issues that are "limited in scope and/or +knowledge of Zig internals." + +### Editing Source Code + +First, build the Stage 1 compiler as described in [the Building section](#building). + +When making changes to the standard library, be sure to edit the files in the +`std` directory and not the installed copy in the build directory. If you add a +new file to the standard library, you must also add the file path in +CMakeLists.txt. + +To test changes, do the following from the build directory: + +1. Run `make install` (on POSIX) or + `msbuild -p:Configuration=Release INSTALL.vcxproj` (on Windows). +2. `bin/zig build --build-file ../build.zig test` (on POSIX) or + `bin\zig.exe build --build-file ..\build.zig test` (on Windows). + +That runs the whole test suite, which does a lot of extra testing that you +likely won't always need, and can take upwards of 2 hours. This is what the +CI server runs when you make a pull request. + +To save time, you can add the `--help` option to the `zig build` command and +see what options are available. One of the most helpful ones is +`-Dskip-release`. Adding this option to the command in step 2 above will take +the time down from around 2 hours to about 6 minutes, and this is a good +enough amount of testing before making a pull request. + +Another example is choosing a different set of things to test. For example, +`test-std` instead of `test` will only run the standard library tests, and +not the other ones. Combining this suggestion with the previous one, you could +do this: + +`bin/zig build --build-file ../build.zig test-std -Dskip-release` (on POSIX) or +`bin\zig.exe build --build-file ..\build.zig test-std -Dskip-release` (on Windows). + +This will run only the standard library tests, in debug mode only, for all +targets (it will cross-compile the tests for non-native targets but not run +them). + +When making changes to the compiler source code, the most helpful test step to +run is `test-behavior`. When editing documentation it is `docs`. You can find +this information and more in the `--help` menu. diff --git a/build.zig b/build.zig index 2dc9c671ec..274ea51436 100644 --- a/build.zig +++ b/build.zig @@ -65,6 +65,8 @@ pub fn build(b: *Builder) !void { b.default_step.dependOn(&exe.step); + addLibUserlandStep(b); + const skip_release = b.option(bool, "skip-release", "Main test suite skips release builds") orelse false; const skip_release_small = b.option(bool, "skip-release-small", "Main test suite skips release-small builds") orelse skip_release; const skip_release_fast = b.option(bool, "skip-release-fast", "Main test suite skips release-fast builds") orelse skip_release; @@ -380,3 +382,28 @@ const Context = struct { dia_guids_lib: []const u8, llvm: LibraryDep, }; + +fn addLibUserlandStep(b: *Builder) void { + // Sadly macOS requires hacks to work around the buggy MACH-O linker code. + const artifact = if (builtin.os == .macosx) + b.addObject("userland", "src-self-hosted/stage1.zig") + else + b.addStaticLibrary("userland", "src-self-hosted/stage1.zig"); + artifact.disable_gen_h = true; + if (builtin.os == .macosx) { + artifact.disable_stack_probing = true; + } else { + artifact.bundle_compiler_rt = true; + } + artifact.setTarget(builtin.arch, builtin.os, builtin.abi); + artifact.linkSystemLibrary("c"); + const libuserland_step = b.step("libuserland", "Build the userland compiler library for use in stage1"); + libuserland_step.dependOn(&artifact.step); + + const output_dir = b.option( + []const u8, + "output-dir", + "For libuserland step, where to put the output", + ) orelse return; + artifact.setOutputDir(output_dir); +} diff --git a/ci/azure/windows_upload b/ci/azure/windows_upload index 088294a180..8478be0e0b 100755 --- a/ci/azure/windows_upload +++ b/ci/azure/windows_upload @@ -6,7 +6,7 @@ set -e if [ "${BUILD_REASON}" != "PullRequest" ]; then cd "$ZIGBUILDDIR" - rm release/*.lib + rm release/*.exe mv ../LICENSE release/ mv ../zig-cache/langref.html release/ mv release/bin/zig.exe release/ diff --git a/cmake/Findclang.cmake b/cmake/Findclang.cmake index 6747e489b4..5555485d89 100644 --- a/cmake/Findclang.cmake +++ b/cmake/Findclang.cmake @@ -44,7 +44,7 @@ else() /usr/local/llvm80/include /mingw64/include) - macro(FIND_AND_ADD_CLANG_LIB _libname_) + macro(FIND_AND_ADD_CLANG_LIB _libname_) string(TOUPPER ${_libname_} _prettylibname_) find_library(CLANG_${_prettylibname_}_LIB NAMES ${_libname_} PATHS diff --git a/deps/lld/wasm/OutputSections.cpp b/deps/lld/wasm/OutputSections.cpp index 4123d63b74..6b7b18d4ca 100644 --- a/deps/lld/wasm/OutputSections.cpp +++ b/deps/lld/wasm/OutputSections.cpp @@ -111,8 +111,8 @@ void CodeSection::writeTo(uint8_t *Buf) { memcpy(Buf, CodeSectionHeader.data(), CodeSectionHeader.size()); // Write code section bodies - parallelForEach(Functions, - [&](const InputChunk *Chunk) { Chunk->writeTo(Buf); }); + for (const InputChunk *Chunk : Functions) + Chunk->writeTo(Buf); } uint32_t CodeSection::numRelocations() const { @@ -176,7 +176,7 @@ void DataSection::writeTo(uint8_t *Buf) { // Write data section headers memcpy(Buf, DataSectionHeader.data(), DataSectionHeader.size()); - parallelForEach(Segments, [&](const OutputSegment *Segment) { + for (const OutputSegment *Segment : Segments) { // Write data segment header uint8_t *SegStart = Buf + Segment->SectionOffset; memcpy(SegStart, Segment->Header.data(), Segment->Header.size()); @@ -184,7 +184,7 @@ void DataSection::writeTo(uint8_t *Buf) { // Write segment data payload for (const InputChunk *Chunk : Segment->InputSegments) Chunk->writeTo(Buf); - }); + } } uint32_t DataSection::numRelocations() const { @@ -232,8 +232,8 @@ void CustomSection::writeTo(uint8_t *Buf) { Buf += NameData.size(); // Write custom sections payload - parallelForEach(InputSections, - [&](const InputSection *Section) { Section->writeTo(Buf); }); + for (const InputSection *Section : InputSections) + Section->writeTo(Buf); } uint32_t CustomSection::numRelocations() const { diff --git a/doc/docgen.zig b/doc/docgen.zig index 0c859e4a15..75c80cc528 100644 --- a/doc/docgen.zig +++ b/doc/docgen.zig @@ -265,6 +265,7 @@ const SeeAlsoItem = struct { const ExpectedOutcome = enum { Succeed, Fail, + BuildFail, }; const Code = struct { @@ -468,6 +469,8 @@ fn genToc(allocator: *mem.Allocator, tokenizer: *Tokenizer) !Toc { code_kind_id = Code.Id{ .Exe = ExpectedOutcome.Succeed }; } else if (mem.eql(u8, code_kind_str, "exe_err")) { code_kind_id = Code.Id{ .Exe = ExpectedOutcome.Fail }; + } else if (mem.eql(u8, code_kind_str, "exe_build_err")) { + code_kind_id = Code.Id{ .Exe = ExpectedOutcome.BuildFail }; } else if (mem.eql(u8, code_kind_str, "test")) { code_kind_id = Code.Id.Test; } else if (mem.eql(u8, code_kind_str, "test_err")) { @@ -509,6 +512,10 @@ fn genToc(allocator: *mem.Allocator, tokenizer: *Tokenizer) !Toc { target_str = "x86_64-windows"; } else if (mem.eql(u8, end_tag_name, "target_linux_x86_64")) { target_str = "x86_64-linux"; + } else if (mem.eql(u8, end_tag_name, "target_wasm")) { + target_str = "wasm32-freestanding"; + } else if (mem.eql(u8, end_tag_name, "target_wasi")) { + target_str = "wasm32-wasi"; } else if (mem.eql(u8, end_tag_name, "link_libc")) { link_libc = true; } else if (mem.eql(u8, end_tag_name, "code_end")) { @@ -1025,6 +1032,8 @@ fn genHtml(allocator: *mem.Allocator, tokenizer: *Tokenizer, toc: *Toc, out: var tmp_dir_name, "--name", code.name, + "--color", + "on", }); try out.print("
$ zig build-exe {}.zig", code.name);
                         switch (code.mode) {
@@ -1059,14 +1068,52 @@ fn genHtml(allocator: *mem.Allocator, tokenizer: *Tokenizer, toc: *Toc, out: var
                         }
                         if (code.target_str) |triple| {
                             try build_args.appendSlice([][]const u8{ "-target", triple });
+                            if (!code.is_inline) {
+                                try out.print(" -target {}", triple);
+                            }
+                        }
+                        if (expected_outcome == .BuildFail) {
+                            const result = try os.ChildProcess.exec(
+                                allocator,
+                                build_args.toSliceConst(),
+                                null,
+                                &env_map,
+                                max_doc_file_size,
+                            );
+                            switch (result.term) {
+                                os.ChildProcess.Term.Exited => |exit_code| {
+                                    if (exit_code == 0) {
+                                        warn("{}\nThe following command incorrectly succeeded:\n", result.stderr);
+                                        for (build_args.toSliceConst()) |arg|
+                                            warn("{} ", arg)
+                                        else
+                                            warn("\n");
+                                        return parseError(tokenizer, code.source_token, "example incorrectly compiled");
+                                    }
+                                },
+                                else => {
+                                    warn("{}\nThe following command crashed:\n", result.stderr);
+                                    for (build_args.toSliceConst()) |arg|
+                                        warn("{} ", arg)
+                                    else
+                                        warn("\n");
+                                    return parseError(tokenizer, code.source_token, "example compile crashed");
+                                },
+                            }
+                            const escaped_stderr = try escapeHtml(allocator, result.stderr);
+                            const colored_stderr = try termColor(allocator, escaped_stderr);
+                            try out.print("\n{}
\n", colored_stderr); + break :code_block; } _ = exec(allocator, &env_map, build_args.toSliceConst()) catch return parseError(tokenizer, code.source_token, "example failed to compile"); if (code.target_str) |triple| { - if (mem.startsWith(u8, triple, "x86_64-linux") and + if (mem.startsWith(u8, triple, "wasm32") or + mem.startsWith(u8, triple, "x86_64-linux") and (builtin.os != builtin.Os.linux or builtin.arch != builtin.Arch.x86_64)) { // skip execution + try out.print("\n"); break :code_block; } } @@ -1130,6 +1177,7 @@ fn genHtml(allocator: *mem.Allocator, tokenizer: *Tokenizer, toc: *Toc, out: var } if (code.target_str) |triple| { try test_args.appendSlice([][]const u8{ "-target", triple }); + try out.print(" -target {}", triple); } const result = exec(allocator, &env_map, test_args.toSliceConst()) catch return parseError(tokenizer, code.source_token, "test failed"); const escaped_stderr = try escapeHtml(allocator, result.stderr); @@ -1310,6 +1358,11 @@ fn genHtml(allocator: *mem.Allocator, tokenizer: *Tokenizer, toc: *Toc, out: var }, } + if (code.target_str) |triple| { + try build_args.appendSlice([][]const u8{ "-target", triple }); + try out.print(" -target {}", triple); + } + if (maybe_error_match) |error_match| { const result = try os.ChildProcess.exec(allocator, build_args.toSliceConst(), null, &env_map, max_doc_file_size); switch (result.term) { diff --git a/doc/langref.html.in b/doc/langref.html.in index bb2a67c562..807fe7d6db 100644 --- a/doc/langref.html.in +++ b/doc/langref.html.in @@ -4,11 +4,12 @@ Documentation - The Zig Programming Language +