Skip to content

Commit 39c7bd2

Browse files
committed
port most of main.cpp to self hosted compiler
1 parent 760b307 commit 39c7bd2

File tree

10 files changed

+1156
-78
lines changed

10 files changed

+1156
-78
lines changed

README.md

+29-24
Original file line numberDiff line numberDiff line change
@@ -119,31 +119,22 @@ libc. Create demo games using Zig.
119119
[![Build Status](https://travis-ci.org/zig-lang/zig.svg?branch=master)](https://travis-ci.org/zig-lang/zig)
120120
[![Build status](https://ci.appveyor.com/api/projects/status/4t80mk2dmucrc38i/branch/master?svg=true)](https://ci.appveyor.com/project/andrewrk/zig-d3l86/branch/master)
121121

122-
### Dependencies
122+
### Stage 1: Build Zig from C++ Source Code
123123

124-
#### Build Dependencies
125-
126-
These compile tools must be available on your system and are used to build
127-
the Zig compiler itself:
124+
#### Dependencies
128125

129126
##### POSIX
130127

131128
* gcc >= 5.0.0 or clang >= 3.6.0
132129
* cmake >= 2.8.5
130+
* LLVM, Clang, LLD libraries == 5.x, compiled with the same gcc or clang version above
133131

134132
##### Windows
135133

136134
* Microsoft Visual Studio 2015
135+
* LLVM, Clang, LLD libraries == 5.x, compiled with the same MSVC version above
137136

138-
#### Library Dependencies
139-
140-
These libraries must be installed on your system, with the development files
141-
available. The Zig compiler links against them. You have to use the same
142-
compiler for these libraries as you do to compile Zig.
143-
144-
* LLVM, Clang, and LLD libraries == 5.x
145-
146-
### Debug / Development Build
137+
#### Instructions
147138

148139
If you have gcc or clang installed, you can find out what `ZIG_LIBC_LIB_DIR`,
149140
`ZIG_LIBC_STATIC_LIB_DIR`, and `ZIG_LIBC_INCLUDE_DIR` should be set to
@@ -158,7 +149,7 @@ make install
158149
./zig build --build-file ../build.zig test
159150
```
160151

161-
#### MacOS
152+
##### MacOS
162153

163154
`ZIG_LIBC_LIB_DIR` and `ZIG_LIBC_STATIC_LIB_DIR` are unused.
164155

@@ -172,21 +163,35 @@ make install
172163
./zig build --build-file ../build.zig test
173164
```
174165

175-
#### Windows
166+
##### Windows
176167

177168
See https://github.com/zig-lang/zig/wiki/Building-Zig-on-Windows
178169

179-
### Release / Install Build
170+
### Stage 2: Build Self-Hosted Zig from Zig Source Code
180171

181-
Once installed, `ZIG_LIBC_LIB_DIR` and `ZIG_LIBC_INCLUDE_DIR` can be overridden
182-
by the `--libc-lib-dir` and `--libc-include-dir` parameters to the zig binary.
172+
*Note: Stage 2 compiler is not complete. Beta users of Zig should use the
173+
Stage 1 compiler for now.*
174+
175+
Dependencies are the same as Stage 1, except now you have a working zig compiler.
183176

184177
```
185-
mkdir build
186-
cd build
187-
cmake .. -DCMAKE_BUILD_TYPE=Release -DZIG_LIBC_LIB_DIR=/some/path -DZIG_LIBC_INCLUDE_DIR=/some/path -DZIG_LIBC_STATIC_INCLUDE_DIR=/some/path
188-
make
189-
sudo make install
178+
bin/zig build --build-file ../build.zig --prefix $(pwd)/stage2 install
179+
```
180+
181+
### Stage 3: Rebuild Self-Hosted Zig Using the Self-Hosted Compiler
182+
183+
This is the actual compiler binary that we will install to the system.
184+
185+
#### Debug / Development Build
186+
187+
```
188+
./stage2/bin/zig build --build-file ../build.zig --prefix $(pwd)/stage3 install
189+
```
190+
191+
#### Release / Install Build
192+
193+
```
194+
./stage2/bin/zig build --build-file ../build.zig install -Drelease-fast
190195
```
191196

192197
### Test Coverage

build.zig

+136-9
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,18 @@ pub fn build(b: &Builder) {
3232
docs_step.dependOn(&docgen_cmd.step);
3333
docs_step.dependOn(&docgen_home_cmd.step);
3434

35-
var exe = b.addExecutable("zig", "src-self-hosted/main.zig");
36-
exe.setBuildMode(mode);
37-
exe.linkSystemLibrary("c");
38-
dependOnLib(exe, findLLVM(b));
35+
if (findLLVM(b)) |llvm| {
36+
var exe = b.addExecutable("zig", "src-self-hosted/main.zig");
37+
exe.setBuildMode(mode);
38+
exe.linkSystemLibrary("c");
39+
dependOnLib(exe, llvm);
3940

40-
b.default_step.dependOn(&exe.step);
41-
b.default_step.dependOn(docs_step);
41+
b.default_step.dependOn(&exe.step);
42+
b.default_step.dependOn(docs_step);
4243

43-
b.installArtifact(exe);
44+
b.installArtifact(exe);
45+
installStdLib(b);
46+
}
4447

4548

4649
const test_filter = b.option([]const u8, "test-filter", "Skip tests that do not match filter");
@@ -91,7 +94,7 @@ const LibraryDep = struct {
9194
includes: ArrayList([]const u8),
9295
};
9396

94-
fn findLLVM(b: &Builder) -> LibraryDep {
97+
fn findLLVM(b: &Builder) -> ?LibraryDep {
9598
const llvm_config_exe = b.findProgram(
9699
[][]const u8{"llvm-config-5.0", "llvm-config"},
97100
[][]const u8{
@@ -102,7 +105,8 @@ fn findLLVM(b: &Builder) -> LibraryDep {
102105
"C:/Libraries/llvm-5.0.0/bin",
103106
}) %% |err|
104107
{
105-
std.debug.panic("unable to find llvm-config: {}\n", err);
108+
warn("unable to find llvm-config: {}\n", err);
109+
return null;
106110
};
107111
const libs_output = b.exec([][]const u8{llvm_config_exe, "--libs", "--system-libs"});
108112
const includes_output = b.exec([][]const u8{llvm_config_exe, "--includedir"});
@@ -143,3 +147,126 @@ fn findLLVM(b: &Builder) -> LibraryDep {
143147
}
144148
return result;
145149
}
150+
151+
pub fn installStdLib(b: &Builder) {
152+
const stdlib_files = []const []const u8 {
153+
"array_list.zig",
154+
"base64.zig",
155+
"buf_map.zig",
156+
"buf_set.zig",
157+
"buffer.zig",
158+
"build.zig",
159+
"c/darwin.zig",
160+
"c/index.zig",
161+
"c/linux.zig",
162+
"c/windows.zig",
163+
"cstr.zig",
164+
"debug.zig",
165+
"dwarf.zig",
166+
"elf.zig",
167+
"empty.zig",
168+
"endian.zig",
169+
"fmt/errol/enum3.zig",
170+
"fmt/errol/index.zig",
171+
"fmt/errol/lookup.zig",
172+
"fmt/index.zig",
173+
"hash_map.zig",
174+
"heap.zig",
175+
"index.zig",
176+
"io.zig",
177+
"linked_list.zig",
178+
"math/acos.zig",
179+
"math/acosh.zig",
180+
"math/asin.zig",
181+
"math/asinh.zig",
182+
"math/atan.zig",
183+
"math/atan2.zig",
184+
"math/atanh.zig",
185+
"math/cbrt.zig",
186+
"math/ceil.zig",
187+
"math/copysign.zig",
188+
"math/cos.zig",
189+
"math/cosh.zig",
190+
"math/exp.zig",
191+
"math/exp2.zig",
192+
"math/expm1.zig",
193+
"math/expo2.zig",
194+
"math/fabs.zig",
195+
"math/floor.zig",
196+
"math/fma.zig",
197+
"math/frexp.zig",
198+
"math/hypot.zig",
199+
"math/ilogb.zig",
200+
"math/index.zig",
201+
"math/inf.zig",
202+
"math/isfinite.zig",
203+
"math/isinf.zig",
204+
"math/isnan.zig",
205+
"math/isnormal.zig",
206+
"math/ln.zig",
207+
"math/log.zig",
208+
"math/log10.zig",
209+
"math/log1p.zig",
210+
"math/log2.zig",
211+
"math/modf.zig",
212+
"math/nan.zig",
213+
"math/pow.zig",
214+
"math/round.zig",
215+
"math/scalbn.zig",
216+
"math/signbit.zig",
217+
"math/sin.zig",
218+
"math/sinh.zig",
219+
"math/sqrt.zig",
220+
"math/tan.zig",
221+
"math/tanh.zig",
222+
"math/trunc.zig",
223+
"mem.zig",
224+
"net.zig",
225+
"os/child_process.zig",
226+
"os/darwin.zig",
227+
"os/darwin_errno.zig",
228+
"os/get_user_id.zig",
229+
"os/index.zig",
230+
"os/linux.zig",
231+
"os/linux_errno.zig",
232+
"os/linux_i386.zig",
233+
"os/linux_x86_64.zig",
234+
"os/path.zig",
235+
"os/windows/error.zig",
236+
"os/windows/index.zig",
237+
"os/windows/util.zig",
238+
"rand.zig",
239+
"sort.zig",
240+
"special/bootstrap.zig",
241+
"special/bootstrap_lib.zig",
242+
"special/build_file_template.zig",
243+
"special/build_runner.zig",
244+
"special/builtin.zig",
245+
"special/compiler_rt/aulldiv.zig",
246+
"special/compiler_rt/aullrem.zig",
247+
"special/compiler_rt/comparetf2.zig",
248+
"special/compiler_rt/fixuint.zig",
249+
"special/compiler_rt/fixunsdfdi.zig",
250+
"special/compiler_rt/fixunsdfsi.zig",
251+
"special/compiler_rt/fixunsdfti.zig",
252+
"special/compiler_rt/fixunssfdi.zig",
253+
"special/compiler_rt/fixunssfsi.zig",
254+
"special/compiler_rt/fixunssfti.zig",
255+
"special/compiler_rt/fixunstfdi.zig",
256+
"special/compiler_rt/fixunstfsi.zig",
257+
"special/compiler_rt/fixunstfti.zig",
258+
"special/compiler_rt/index.zig",
259+
"special/compiler_rt/udivmod.zig",
260+
"special/compiler_rt/udivmoddi4.zig",
261+
"special/compiler_rt/udivmodti4.zig",
262+
"special/compiler_rt/udivti3.zig",
263+
"special/compiler_rt/umodti3.zig",
264+
"special/panic.zig",
265+
"special/test_runner.zig",
266+
};
267+
for (stdlib_files) |stdlib_file| {
268+
const src_path = %%os.path.join(b.allocator, "std", stdlib_file);
269+
const dest_path = %%os.path.join(b.allocator, "lib", "zig", "std", stdlib_file);
270+
b.installFile(src_path, dest_path);
271+
}
272+
}

src-self-hosted/llvm.zig

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const builtin = @import("builtin");
2+
const c = @import("c.zig");
3+
const assert = @import("std").debug.assert;
4+
5+
pub const ValueRef = removeNullability(c.LLVMValueRef);
6+
pub const ModuleRef = removeNullability(c.LLVMModuleRef);
7+
pub const ContextRef = removeNullability(c.LLVMContextRef);
8+
pub const BuilderRef = removeNullability(c.LLVMBuilderRef);
9+
10+
fn removeNullability(comptime T: type) -> type {
11+
comptime assert(@typeId(T) == builtin.TypeId.Nullable);
12+
return T.Child;
13+
}

0 commit comments

Comments
 (0)