authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-09-18 02:50:51-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-09-18 02:51:09-04:00
log385da95eb48d1e9e836fec1c3882837847539009
treef49181481669492b67a52c9f7136da99d00e3c7f
parentdbc202cc6a3bcaffb2f6528551d6dd0a62a6b5a3

std.build: simpler API

merge LibExeObj and CLibExeObj also make it so that you can disable libc when compiling C

1 files changed, 419 insertions(+), 454 deletions(-)

std/build.zig+419-454
...@@ -164,20 +164,20 @@ pub const Builder = struct {...@@ -164,20 +164,20 @@ pub const Builder = struct {
164 return obj_step;164 return obj_step;
165 }165 }
166166
167 pub fn addCStaticLibrary(self: &Builder, name: []const u8) -> &CLibExeObjStep {167 pub fn addCStaticLibrary(self: &Builder, name: []const u8) -> &LibExeObjStep {
168 return CLibExeObjStep.createStaticLibrary(self, name);168 return LibExeObjStep.createCStaticLibrary(self, name);
169 }169 }
170170
171 pub fn addCSharedLibrary(self: &Builder, name: []const u8, ver: &const Version) -> &CLibExeObjStep {171 pub fn addCSharedLibrary(self: &Builder, name: []const u8, ver: &const Version) -> &LibExeObjStep {
172 return CLibExeObjStep.createSharedLibrary(self, name, ver);172 return LibExeObjStep.createCSharedLibrary(self, name, ver);
173 }173 }
174174
175 pub fn addCExecutable(self: &Builder, name: []const u8) -> &CLibExeObjStep {175 pub fn addCExecutable(self: &Builder, name: []const u8) -> &LibExeObjStep {
176 return CLibExeObjStep.createExecutable(self, name);176 return LibExeObjStep.createCExecutable(self, name);
177 }177 }
178178
179 pub fn addCObject(self: &Builder, name: []const u8, src: []const u8) -> &CLibExeObjStep {179 pub fn addCObject(self: &Builder, name: []const u8, src: []const u8) -> &LibExeObjStep {
180 return CLibExeObjStep.createObject(self, name, src);180 return LibExeObjStep.createCObject(self, name, src);
181 }181 }
182182
183 /// ::args are copied.183 /// ::args are copied.
...@@ -581,16 +581,8 @@ pub const Builder = struct {...@@ -581,16 +581,8 @@ pub const Builder = struct {
581 self.getInstallStep().dependOn(&self.addInstallArtifact(artifact).step);581 self.getInstallStep().dependOn(&self.addInstallArtifact(artifact).step);
582 }582 }
583583
584 pub fn addInstallArtifact(self: &Builder, artifact: &LibExeObjStep) -> &InstallArtifactStep(LibExeObjStep) {584 pub fn addInstallArtifact(self: &Builder, artifact: &LibExeObjStep) -> &InstallArtifactStep {
585 return InstallArtifactStep(LibExeObjStep).create(self, artifact);585 return InstallArtifactStep.create(self, artifact);
586 }
587
588 pub fn installCArtifact(self: &Builder, artifact: &CLibExeObjStep) {
589 self.getInstallStep().dependOn(&self.addInstallCArtifact(artifact).step);
590 }
591
592 pub fn addInstallCArtifact(self: &Builder, artifact: &CLibExeObjStep) -> &InstallArtifactStep(CLibExeObjStep) {
593 return InstallArtifactStep(CLibExeObjStep).create(self, artifact);
594 }586 }
595587
596 ///::dest_rel_path is relative to prefix path or it can be an absolute path588 ///::dest_rel_path is relative to prefix path or it can be an absolute path
...@@ -685,26 +677,39 @@ const Target = enum {...@@ -685,26 +677,39 @@ const Target = enum {
685pub const LibExeObjStep = struct {677pub const LibExeObjStep = struct {
686 step: Step,678 step: Step,
687 builder: &Builder,679 builder: &Builder,
688 root_src: ?[]const u8,
689 name: []const u8,680 name: []const u8,
690 target: Target,681 target: Target,
691 linker_script: ?[]const u8,
692 link_libs: BufSet,682 link_libs: BufSet,
693 verbose: bool,683 linker_script: ?[]const u8,
694 build_mode: builtin.Mode,684 out_filename: []const u8,
695 static: bool,
696 output_path: ?[]const u8,685 output_path: ?[]const u8,
697 output_h_path: ?[]const u8,686 static: bool,
698 kind: Kind,
699 version: Version,687 version: Version,
700 out_h_filename: []const u8,688 object_files: ArrayList([]const u8),
701 out_filename: []const u8,689 build_mode: builtin.Mode,
690 kind: Kind,
702 major_only_filename: []const u8,691 major_only_filename: []const u8,
703 name_only_filename: []const u8,692 name_only_filename: []const u8,
704 object_files: ArrayList([]const u8),693 strip: bool,
694 full_path_libs: ArrayList([]const u8),
695 need_flat_namespace_hack: bool,
696 is_zig: bool,
697 cflags: ArrayList([]const u8),
698 include_dirs: ArrayList([]const u8),
699 disable_libc: bool,
700
701 // zig only stuff
702 root_src: ?[]const u8,
703 verbose: bool,
704 output_h_path: ?[]const u8,
705 out_h_filename: []const u8,
705 assembly_files: ArrayList([]const u8),706 assembly_files: ArrayList([]const u8),
706 packages: ArrayList(Pkg),707 packages: ArrayList(Pkg),
707708
709 // C only stuff
710 source_files: ArrayList([]const u8),
711 object_src: []const u8,
712
708 const Pkg = struct {713 const Pkg = struct {
709 name: []const u8,714 name: []const u8,
710 path: []const u8,715 path: []const u8,
...@@ -724,28 +729,54 @@ pub const LibExeObjStep = struct {...@@ -724,28 +729,54 @@ pub const LibExeObjStep = struct {
724 return self;729 return self;
725 }730 }
726731
732 pub fn createCSharedLibrary(builder: &Builder, name: []const u8, version: &const Version) -> &LibExeObjStep {
733 const self = %%builder.allocator.create(LibExeObjStep);
734 *self = initC(builder, name, Kind.Lib, version, false);
735 return self;
736 }
737
727 pub fn createStaticLibrary(builder: &Builder, name: []const u8, root_src: ?[]const u8) -> &LibExeObjStep {738 pub fn createStaticLibrary(builder: &Builder, name: []const u8, root_src: ?[]const u8) -> &LibExeObjStep {
728 const self = %%builder.allocator.create(LibExeObjStep);739 const self = %%builder.allocator.create(LibExeObjStep);
729 *self = initExtraArgs(builder, name, root_src, Kind.Lib, true, builder.version(0, 0, 0));740 *self = initExtraArgs(builder, name, root_src, Kind.Lib, true, builder.version(0, 0, 0));
730 return self;741 return self;
731 }742 }
732743
744 pub fn createCStaticLibrary(builder: &Builder, name: []const u8) -> &LibExeObjStep {
745 const self = %%builder.allocator.create(LibExeObjStep);
746 *self = initC(builder, name, Kind.Lib, builder.version(0, 0, 0), true);
747 return self;
748 }
749
733 pub fn createObject(builder: &Builder, name: []const u8, root_src: []const u8) -> &LibExeObjStep {750 pub fn createObject(builder: &Builder, name: []const u8, root_src: []const u8) -> &LibExeObjStep {
734 const self = %%builder.allocator.create(LibExeObjStep);751 const self = %%builder.allocator.create(LibExeObjStep);
735 *self = initExtraArgs(builder, name, root_src, Kind.Obj, false, builder.version(0, 0, 0));752 *self = initExtraArgs(builder, name, root_src, Kind.Obj, false, builder.version(0, 0, 0));
736 return self;753 return self;
737 }754 }
738755
756 pub fn createCObject(builder: &Builder, name: []const u8, src: []const u8) -> &LibExeObjStep {
757 const self = %%builder.allocator.create(LibExeObjStep);
758 *self = initC(builder, name, Kind.Obj, builder.version(0, 0, 0), false);
759 self.object_src = src;
760 return self;
761 }
762
739 pub fn createExecutable(builder: &Builder, name: []const u8, root_src: ?[]const u8) -> &LibExeObjStep {763 pub fn createExecutable(builder: &Builder, name: []const u8, root_src: ?[]const u8) -> &LibExeObjStep {
740 const self = %%builder.allocator.create(LibExeObjStep);764 const self = %%builder.allocator.create(LibExeObjStep);
741 *self = initExtraArgs(builder, name, root_src, Kind.Exe, false, builder.version(0, 0, 0));765 *self = initExtraArgs(builder, name, root_src, Kind.Exe, false, builder.version(0, 0, 0));
742 return self;766 return self;
743 }767 }
744768
769 pub fn createCExecutable(builder: &Builder, name: []const u8) -> &LibExeObjStep {
770 const self = %%builder.allocator.create(LibExeObjStep);
771 *self = initC(builder, name, Kind.Exe, builder.version(0, 0, 0), false);
772 return self;
773 }
774
745 fn initExtraArgs(builder: &Builder, name: []const u8, root_src: ?[]const u8, kind: Kind,775 fn initExtraArgs(builder: &Builder, name: []const u8, root_src: ?[]const u8, kind: Kind,
746 static: bool, ver: &const Version) -> LibExeObjStep776 static: bool, ver: &const Version) -> LibExeObjStep
747 {777 {
748 var self = LibExeObjStep {778 var self = LibExeObjStep {
779 .strip = false,
749 .builder = builder,780 .builder = builder,
750 .verbose = false,781 .verbose = false,
751 .build_mode = builtin.Mode.Debug,782 .build_mode = builtin.Mode.Debug,
...@@ -767,6 +798,52 @@ pub const LibExeObjStep = struct {...@@ -767,6 +798,52 @@ pub const LibExeObjStep = struct {
767 .object_files = ArrayList([]const u8).init(builder.allocator),798 .object_files = ArrayList([]const u8).init(builder.allocator),
768 .assembly_files = ArrayList([]const u8).init(builder.allocator),799 .assembly_files = ArrayList([]const u8).init(builder.allocator),
769 .packages = ArrayList(Pkg).init(builder.allocator),800 .packages = ArrayList(Pkg).init(builder.allocator),
801 .is_zig = true,
802 .full_path_libs = ArrayList([]const u8).init(builder.allocator),
803 .need_flat_namespace_hack = false,
804 .cflags = ArrayList([]const u8).init(builder.allocator),
805 .source_files = undefined,
806 .include_dirs = ArrayList([]const u8).init(builder.allocator),
807 .object_src = undefined,
808 .disable_libc = true,
809 };
810 self.computeOutFileNames();
811 return self;
812 }
813
814 fn initC(builder: &Builder, name: []const u8, kind: Kind, version: &const Version, static: bool) -> LibExeObjStep {
815 var self = LibExeObjStep {
816 .builder = builder,
817 .name = name,
818 .kind = kind,
819 .version = *version,
820 .static = static,
821 .target = Target.Native,
822 .cflags = ArrayList([]const u8).init(builder.allocator),
823 .source_files = ArrayList([]const u8).init(builder.allocator),
824 .object_files = ArrayList([]const u8).init(builder.allocator),
825 .step = Step.init(name, builder.allocator, make),
826 .link_libs = BufSet.init(builder.allocator),
827 .full_path_libs = ArrayList([]const u8).init(builder.allocator),
828 .include_dirs = ArrayList([]const u8).init(builder.allocator),
829 .output_path = null,
830 .out_filename = undefined,
831 .major_only_filename = undefined,
832 .name_only_filename = undefined,
833 .object_src = undefined,
834 .build_mode = builtin.Mode.Debug,
835 .strip = false,
836 .need_flat_namespace_hack = false,
837 .disable_libc = false,
838 .is_zig = false,
839 .linker_script = null,
840
841 .root_src = undefined,
842 .verbose = undefined,
843 .output_h_path = undefined,
844 .out_h_filename = undefined,
845 .assembly_files = undefined,
846 .packages = undefined,
770 };847 };
771 self.computeOutFileNames();848 self.computeOutFileNames();
772 return self;849 return self;
...@@ -823,14 +900,35 @@ pub const LibExeObjStep = struct {...@@ -823,14 +900,35 @@ pub const LibExeObjStep = struct {
823 self.computeOutFileNames();900 self.computeOutFileNames();
824 }901 }
825902
903 // TODO respect this in the C args
826 pub fn setLinkerScriptPath(self: &LibExeObjStep, path: []const u8) {904 pub fn setLinkerScriptPath(self: &LibExeObjStep, path: []const u8) {
827 self.linker_script = path;905 self.linker_script = path;
828 }906 }
829907
908 pub fn linkLibrary(self: &LibExeObjStep, lib: &LibExeObjStep) {
909 assert(self.kind != Kind.Obj);
910 assert(lib.kind == Kind.Lib);
911
912 self.step.dependOn(&lib.step);
913
914 %%self.full_path_libs.append(lib.getOutputPath());
915
916 // TODO should be some kind of isolated directory that only has this header in it
917 %%self.include_dirs.append(self.builder.cache_root);
918 self.need_flat_namespace_hack = true;
919 }
920
830 pub fn linkSystemLibrary(self: &LibExeObjStep, name: []const u8) {921 pub fn linkSystemLibrary(self: &LibExeObjStep, name: []const u8) {
922 assert(self.kind != Kind.Obj);
831 %%self.link_libs.put(name);923 %%self.link_libs.put(name);
832 }924 }
833925
926 pub fn addSourceFile(self: &LibExeObjStep, file: []const u8) {
927 assert(self.kind != Kind.Obj);
928 assert(!self.is_zig);
929 %%self.source_files.append(file);
930 }
931
834 pub fn setVerbose(self: &LibExeObjStep, value: bool) {932 pub fn setVerbose(self: &LibExeObjStep, value: bool) {
835 self.verbose = value;933 self.verbose = value;
836 }934 }
...@@ -880,21 +978,53 @@ pub const LibExeObjStep = struct {...@@ -880,21 +978,53 @@ pub const LibExeObjStep = struct {
880 self.step.dependOn(&obj.step);978 self.step.dependOn(&obj.step);
881979
882 %%self.object_files.append(obj.getOutputPath());980 %%self.object_files.append(obj.getOutputPath());
981
982 // TODO make this lazy instead of stateful
983 if (!obj.disable_libc) {
984 self.disable_libc = false;
985 }
986
987 // TODO should be some kind of isolated directory that only has this header in it
988 %%self.include_dirs.append(self.builder.cache_root);
989 }
990
991 // TODO put include_dirs in zig command line
992 pub fn addIncludeDir(self: &LibExeObjStep, path: []const u8) {
993 %%self.include_dirs.append(path);
883 }994 }
884995
885 pub fn addPackagePath(self: &LibExeObjStep, name: []const u8, pkg_index_path: []const u8) {996 pub fn addPackagePath(self: &LibExeObjStep, name: []const u8, pkg_index_path: []const u8) {
997 assert(self.is_zig);
998
886 %%self.packages.append(Pkg {999 %%self.packages.append(Pkg {
887 .name = name,1000 .name = name,
888 .path = pkg_index_path,1001 .path = pkg_index_path,
889 });1002 });
890 }1003 }
8911004
1005 pub fn addCompileFlags(self: &LibExeObjStep, flags: []const []const u8) {
1006 for (flags) |flag| {
1007 %%self.cflags.append(flag);
1008 }
1009 }
1010
1011 pub fn setNoStdLib(self: &LibExeObjStep, disable: bool) {
1012 assert(!self.is_zig);
1013 self.disable_libc = disable;
1014 }
1015
892 fn make(step: &Step) -> %void {1016 fn make(step: &Step) -> %void {
893 const self = @fieldParentPtr(LibExeObjStep, "step", step);1017 const self = @fieldParentPtr(LibExeObjStep, "step", step);
1018 return if (self.is_zig) self.makeZig() else self.makeC();
1019 }
1020
1021 fn makeZig(self: &LibExeObjStep) -> %void {
894 const builder = self.builder;1022 const builder = self.builder;
8951023
1024 assert(self.is_zig);
1025
896 if (self.root_src == null and self.object_files.len == 0 and self.assembly_files.len == 0) {1026 if (self.root_src == null and self.object_files.len == 0 and self.assembly_files.len == 0) {
897 %%io.stderr.printf("{}: linker needs 1 or more objects to link\n", step.name);1027 %%io.stderr.printf("{}: linker needs 1 or more objects to link\n", self.step.name);
898 return error.NeedAnObject;1028 return error.NeedAnObject;
899 }1029 }
9001030
...@@ -926,6 +1056,10 @@ pub const LibExeObjStep = struct {...@@ -926,6 +1056,10 @@ pub const LibExeObjStep = struct {
926 %%zig_args.append("--verbose");1056 %%zig_args.append("--verbose");
927 }1057 }
9281058
1059 if (self.strip) {
1060 %%zig_args.append("--strip");
1061 }
1062
929 switch (self.build_mode) {1063 switch (self.build_mode) {
930 builtin.Mode.Debug => {},1064 builtin.Mode.Debug => {},
931 builtin.Mode.ReleaseSafe => %%zig_args.append("--release-safe"),1065 builtin.Mode.ReleaseSafe => %%zig_args.append("--release-safe"),
...@@ -987,6 +1121,11 @@ pub const LibExeObjStep = struct {...@@ -987,6 +1121,11 @@ pub const LibExeObjStep = struct {
987 }1121 }
988 }1122 }
9891123
1124 if (!self.disable_libc) {
1125 %%zig_args.append("--library");
1126 %%zig_args.append("c");
1127 }
1128
990 for (self.packages.toSliceConst()) |pkg| {1129 for (self.packages.toSliceConst()) |pkg| {
991 %%zig_args.append("--pkg-begin");1130 %%zig_args.append("--pkg-begin");
992 %%zig_args.append(pkg.name);1131 %%zig_args.append(pkg.name);
...@@ -1009,6 +1148,11 @@ pub const LibExeObjStep = struct {...@@ -1009,6 +1148,11 @@ pub const LibExeObjStep = struct {
1009 %%zig_args.append(lib_path);1148 %%zig_args.append(lib_path);
1010 }1149 }
10111150
1151 for (self.full_path_libs.toSliceConst()) |full_path_lib| {
1152 %%zig_args.append("--library");
1153 %%zig_args.append(builder.pathFromRoot(full_path_lib));
1154 }
1155
1012 %return builder.spawnChild(builder.zig_exe, zig_args.toSliceConst());1156 %return builder.spawnChild(builder.zig_exe, zig_args.toSliceConst());
10131157
1014 if (self.kind == Kind.Lib and !self.static) {1158 if (self.kind == Kind.Lib and !self.static) {
...@@ -1016,403 +1160,91 @@ pub const LibExeObjStep = struct {...@@ -1016,403 +1160,91 @@ pub const LibExeObjStep = struct {
1016 self.name_only_filename);1160 self.name_only_filename);
1017 }1161 }
1018 }1162 }
1019};
10201163
1021pub const TestStep = struct {1164 fn appendCompileFlags(self: &LibExeObjStep, args: &ArrayList([]const u8)) {
1022 step: Step,1165 if (!self.strip) {
1023 builder: &Builder,1166 %%args.append("-g");
1024 root_src: []const u8,1167 }
1025 build_mode: builtin.Mode,1168 switch (self.build_mode) {
1026 verbose: bool,1169 builtin.Mode.Debug => {
1027 link_libs: BufSet,1170 if (!self.disable_libc) {
1028 name_prefix: []const u8,1171 %%args.append("-fstack-protector-strong");
1029 filter: ?[]const u8,1172 %%args.append("--param");
1030 target: Target,1173 %%args.append("ssp-buffer-size=4");
1031 exec_cmd_args: ?[]const ?[]const u8,1174 }
1175 },
1176 builtin.Mode.ReleaseSafe => {
1177 %%args.append("-O2");
1178 if (!self.disable_libc) {
1179 %%args.append("-D_FORTIFY_SOURCE=2");
1180 %%args.append("-fstack-protector-strong");
1181 %%args.append("--param");
1182 %%args.append("ssp-buffer-size=4");
1183 }
1184 },
1185 builtin.Mode.ReleaseFast => {
1186 %%args.append("-O2");
1187 },
1188 }
10321189
1033 pub fn init(builder: &Builder, root_src: []const u8) -> TestStep {1190 for (self.include_dirs.toSliceConst()) |dir| {
1034 const step_name = builder.fmt("test {}", root_src);1191 %%args.append("-I");
1035 TestStep {1192 %%args.append(self.builder.pathFromRoot(dir));
1036 .step = Step.init(step_name, builder.allocator, make),
1037 .builder = builder,
1038 .root_src = root_src,
1039 .build_mode = builtin.Mode.Debug,
1040 .verbose = false,
1041 .name_prefix = "",
1042 .filter = null,
1043 .link_libs = BufSet.init(builder.allocator),
1044 .target = Target.Native,
1045 .exec_cmd_args = null,
1046 }1193 }
1047 }
10481194
1049 pub fn setVerbose(self: &TestStep, value: bool) {1195 for (self.cflags.toSliceConst()) |cflag| {
1050 self.verbose = value;1196 %%args.append(cflag);
1051 }1197 }
10521198
1053 pub fn setBuildMode(self: &TestStep, mode: builtin.Mode) {1199 if (self.disable_libc) {
1054 self.build_mode = mode;1200 %%args.append("-nostdlib");
1201 }
1055 }1202 }
10561203
1057 pub fn linkSystemLibrary(self: &TestStep, name: []const u8) {1204 fn makeC(self: &LibExeObjStep) -> %void {
1058 %%self.link_libs.put(name);1205 const cc = os.getEnv("CC") ?? "cc";
1059 }1206 const builder = self.builder;
10601207
1061 pub fn setNamePrefix(self: &TestStep, text: []const u8) {1208 assert(!self.is_zig);
1062 self.name_prefix = text;
1063 }
10641209
1065 pub fn setFilter(self: &TestStep, text: ?[]const u8) {1210 var cc_args = ArrayList([]const u8).init(builder.allocator);
1066 self.filter = text;1211 defer cc_args.deinit();
1067 }
10681212
1069 pub fn setTarget(self: &TestStep, target_arch: builtin.Arch, target_os: builtin.Os,1213 switch (self.kind) {
1070 target_environ: builtin.Environ)1214 Kind.Obj => {
1071 {1215 %%cc_args.append("-c");
1072 self.target = Target.Cross {1216 %%cc_args.append(builder.pathFromRoot(self.object_src));
1073 CrossTarget {
1074 .arch = target_arch,
1075 .os = target_os,
1076 .environ = target_environ,
1077 }
1078 };
1079 }
10801217
1081 pub fn setExecCmd(self: &TestStep, args: []const ?[]const u8) {1218 const output_path = builder.pathFromRoot(self.getOutputPath());
1082 self.exec_cmd_args = args;1219 %%cc_args.append("-o");
1083 }1220 %%cc_args.append(output_path);
10841221
1085 fn make(step: &Step) -> %void {1222 self.appendCompileFlags(&cc_args);
1086 const self = @fieldParentPtr(TestStep, "step", step);
1087 const builder = self.builder;
10881223
1089 var zig_args = ArrayList([]const u8).init(builder.allocator);1224 %return builder.spawnChild(cc, cc_args.toSliceConst());
1090 defer zig_args.deinit();1225 },
1226 Kind.Lib => {
1227 for (self.source_files.toSliceConst()) |source_file| {
1228 %%cc_args.resize(0);
10911229
1092 %%zig_args.append("test");1230 if (!self.static) {
1093 %%zig_args.append(builder.pathFromRoot(self.root_src));1231 %%cc_args.append("-fPIC");
1232 }
10941233
1095 if (self.verbose) {1234 const abs_source_file = builder.pathFromRoot(source_file);
1096 %%zig_args.append("--verbose");1235 %%cc_args.append("-c");
1097 }1236 %%cc_args.append(abs_source_file);
10981237
1099 switch (self.build_mode) {1238 const cache_o_src = %%os.path.join(builder.allocator, builder.cache_root, source_file);
1100 builtin.Mode.Debug => {},1239 const cache_o_dir = os.path.dirname(cache_o_src);
1101 builtin.Mode.ReleaseSafe => %%zig_args.append("--release-safe"),1240 %return builder.makePath(cache_o_dir);
1102 builtin.Mode.ReleaseFast => %%zig_args.append("--release-fast"),1241 const cache_o_file = builder.fmt("{}{}", cache_o_src, self.target.oFileExt());
1103 }1242 %%cc_args.append("-o");
1243 %%cc_args.append(builder.pathFromRoot(cache_o_file));
11041244
1105 switch (self.target) {1245 self.appendCompileFlags(&cc_args);
1106 Target.Native => {},
1107 Target.Cross => |cross_target| {
1108 %%zig_args.append("--target-arch");
1109 %%zig_args.append(@enumTagName(cross_target.arch));
11101246
1111 %%zig_args.append("--target-os");1247 %return builder.spawnChild(cc, cc_args.toSliceConst());
1112 %%zig_args.append(@enumTagName(cross_target.os));
1113
1114 %%zig_args.append("--target-environ");
1115 %%zig_args.append(@enumTagName(cross_target.environ));
1116 },
1117 }
1118
1119 if (self.filter) |filter| {
1120 %%zig_args.append("--test-filter");
1121 %%zig_args.append(filter);
1122 }
1123
1124 if (self.name_prefix.len != 0) {
1125 %%zig_args.append("--test-name-prefix");
1126 %%zig_args.append(self.name_prefix);
1127 }
1128
1129 {
1130 var it = self.link_libs.iterator();
1131 while (true) {
1132 const entry = it.next() ?? break;
1133 %%zig_args.append("--library");
1134 %%zig_args.append(entry.key);
1135 }
1136 }
1137
1138 if (self.exec_cmd_args) |exec_cmd_args| {
1139 for (exec_cmd_args) |cmd_arg| {
1140 if (cmd_arg) |arg| {
1141 %%zig_args.append("--test-cmd");
1142 %%zig_args.append(arg);
1143 } else {
1144 %%zig_args.append("--test-cmd-bin");
1145 }
1146 }
1147 }
1148
1149 for (builder.include_paths.toSliceConst()) |include_path| {
1150 %%zig_args.append("-isystem");
1151 %%zig_args.append(builder.pathFromRoot(include_path));
1152 }
1153
1154 for (builder.rpaths.toSliceConst()) |rpath| {
1155 %%zig_args.append("-rpath");
1156 %%zig_args.append(rpath);
1157 }
1158
1159 for (builder.lib_paths.toSliceConst()) |lib_path| {
1160 %%zig_args.append("--library-path");
1161 %%zig_args.append(lib_path);
1162 }
1163
1164 %return builder.spawnChild(builder.zig_exe, zig_args.toSliceConst());
1165 }
1166};
1167
1168pub const CLibExeObjStep = struct {
1169 step: Step,
1170 name: []const u8,
1171 out_filename: []const u8,
1172 output_path: ?[]const u8,
1173 static: bool,
1174 version: Version,
1175 cflags: ArrayList([]const u8),
1176 source_files: ArrayList([]const u8),
1177 object_files: ArrayList([]const u8),
1178 link_libs: BufSet,
1179 full_path_libs: ArrayList([]const u8),
1180 target: Target,
1181 builder: &Builder,
1182 include_dirs: ArrayList([]const u8),
1183 major_only_filename: []const u8,
1184 name_only_filename: []const u8,
1185 object_src: []const u8,
1186 kind: Kind,
1187 build_mode: builtin.Mode,
1188 strip: bool,
1189 need_flat_namespace_hack: bool,
1190
1191 const Kind = enum {
1192 Exe,
1193 Lib,
1194 Obj,
1195 };
1196
1197 pub fn createSharedLibrary(builder: &Builder, name: []const u8, version: &const Version) -> &CLibExeObjStep {
1198 const self = %%builder.allocator.create(CLibExeObjStep);
1199 *self = init(builder, name, Kind.Lib, version, false);
1200 return self;
1201 }
1202
1203 pub fn createStaticLibrary(builder: &Builder, name: []const u8) -> &CLibExeObjStep {
1204 const self = %%builder.allocator.create(CLibExeObjStep);
1205 *self = init(builder, name, Kind.Lib, builder.version(0, 0, 0), true);
1206 return self;
1207 }
1208
1209 pub fn createObject(builder: &Builder, name: []const u8, src: []const u8) -> &CLibExeObjStep {
1210 const self = %%builder.allocator.create(CLibExeObjStep);
1211 *self = init(builder, name, Kind.Obj, builder.version(0, 0, 0), false);
1212 self.object_src = src;
1213 return self;
1214 }
1215
1216 pub fn createExecutable(builder: &Builder, name: []const u8) -> &CLibExeObjStep {
1217 const self = %%builder.allocator.create(CLibExeObjStep);
1218 *self = init(builder, name, Kind.Exe, builder.version(0, 0, 0), false);
1219 return self;
1220 }
1221
1222 fn init(builder: &Builder, name: []const u8, kind: Kind, version: &const Version, static: bool) -> CLibExeObjStep {
1223 var clib = CLibExeObjStep {
1224 .builder = builder,
1225 .name = name,
1226 .kind = kind,
1227 .version = *version,
1228 .static = static,
1229 .target = Target.Native,
1230 .cflags = ArrayList([]const u8).init(builder.allocator),
1231 .source_files = ArrayList([]const u8).init(builder.allocator),
1232 .object_files = ArrayList([]const u8).init(builder.allocator),
1233 .step = Step.init(name, builder.allocator, make),
1234 .link_libs = BufSet.init(builder.allocator),
1235 .full_path_libs = ArrayList([]const u8).init(builder.allocator),
1236 .include_dirs = ArrayList([]const u8).init(builder.allocator),
1237 .output_path = null,
1238 .out_filename = undefined,
1239 .major_only_filename = undefined,
1240 .name_only_filename = undefined,
1241 .object_src = undefined,
1242 .build_mode = builtin.Mode.Debug,
1243 .strip = false,
1244 .need_flat_namespace_hack = false,
1245 };
1246 clib.computeOutFileNames();
1247 return clib;
1248 }
1249
1250 fn computeOutFileNames(self: &CLibExeObjStep) {
1251 switch (self.kind) {
1252 Kind.Obj => {
1253 self.out_filename = self.builder.fmt("{}{}", self.name, self.target.oFileExt());
1254 },
1255 Kind.Exe => {
1256 self.out_filename = self.builder.fmt("{}{}", self.name, self.target.exeFileExt());
1257 },
1258 Kind.Lib => {
1259 if (self.static) {
1260 self.out_filename = self.builder.fmt("lib{}.a", self.name);
1261 } else {
1262 self.out_filename = self.builder.fmt("lib{}.so.{d}.{d}.{d}",
1263 self.name, self.version.major, self.version.minor, self.version.patch);
1264 self.major_only_filename = self.builder.fmt("lib{}.so.{d}", self.name, self.version.major);
1265 self.name_only_filename = self.builder.fmt("lib{}.so", self.name);
1266 }
1267 },
1268 }
1269 }
1270
1271 pub fn setOutputPath(self: &CLibExeObjStep, value: []const u8) {
1272 self.output_path = value;
1273 }
1274
1275 pub fn getOutputPath(self: &CLibExeObjStep) -> []const u8 {
1276 if (self.output_path) |output_path| {
1277 output_path
1278 } else {
1279 %%os.path.join(self.builder.allocator, self.builder.cache_root, self.out_filename)
1280 }
1281 }
1282
1283 pub fn linkLibrary(self: &CLibExeObjStep, lib: &LibExeObjStep) {
1284 assert(self.kind != Kind.Obj);
1285 assert(lib.kind == LibExeObjStep.Kind.Lib);
1286 self.step.dependOn(&lib.step);
1287 %%self.full_path_libs.append(lib.getOutputPath());
1288 // TODO should be some kind of isolated directory that only has this header in it
1289 %%self.include_dirs.append(self.builder.cache_root);
1290 self.need_flat_namespace_hack = true;
1291 }
1292
1293 pub fn linkSystemLibrary(self: &CLibExeObjStep, name: []const u8) {
1294 assert(self.kind != Kind.Obj);
1295 %%self.link_libs.put(name);
1296 }
1297
1298 pub fn linkCLibrary(self: &CLibExeObjStep, other: &CLibExeObjStep) {
1299 assert(self.kind != Kind.Obj);
1300 assert(other.kind == Kind.Lib);
1301 self.step.dependOn(&other.step);
1302 %%self.full_path_libs.append(other.getOutputPath());
1303 }
1304
1305 pub fn addSourceFile(self: &CLibExeObjStep, file: []const u8) {
1306 assert(self.kind != Kind.Obj);
1307 %%self.source_files.append(file);
1308 }
1309
1310 pub fn addObjectFile(self: &CLibExeObjStep, file: []const u8) {
1311 assert(self.kind != Kind.Obj);
1312 %%self.object_files.append(file);
1313 }
1314
1315 pub fn addObject(self: &CLibExeObjStep, obj: &LibExeObjStep) {
1316 assert(self.kind != Kind.Obj);
1317 assert(obj.kind == LibExeObjStep.Kind.Obj);
1318
1319 self.step.dependOn(&obj.step);
1320
1321 %%self.object_files.append(obj.getOutputPath());
1322
1323 // TODO should be some kind of isolated directory that only has this header in it
1324 %%self.include_dirs.append(self.builder.cache_root);
1325 }
1326
1327 pub fn addIncludeDir(self: &CLibExeObjStep, path: []const u8) {
1328 %%self.include_dirs.append(path);
1329 }
1330
1331 pub fn setBuildMode(self: &CLibExeObjStep, build_mode: builtin.Mode) {
1332 self.build_mode = build_mode;
1333 }
1334
1335 pub fn addCompileFlags(self: &CLibExeObjStep, flags: []const []const u8) {
1336 for (flags) |flag| {
1337 %%self.cflags.append(flag);
1338 }
1339 }
1340
1341 fn appendCompileFlags(self: &CLibExeObjStep, args: &ArrayList([]const u8)) {
1342 if (!self.strip) {
1343 %%args.append("-g");
1344 }
1345 switch (self.build_mode) {
1346 builtin.Mode.Debug => {
1347 %%args.append("-fstack-protector-strong");
1348 %%args.append("--param");
1349 %%args.append("ssp-buffer-size=4");
1350 },
1351 builtin.Mode.ReleaseSafe => {
1352 %%args.append("-O2");
1353 %%args.append("-D_FORTIFY_SOURCE=2");
1354 %%args.append("-fstack-protector-strong");
1355 %%args.append("--param");
1356 %%args.append("ssp-buffer-size=4");
1357 },
1358 builtin.Mode.ReleaseFast => {
1359 %%args.append("-O2");
1360 },
1361 }
1362
1363 for (self.include_dirs.toSliceConst()) |dir| {
1364 %%args.append("-I");
1365 %%args.append(self.builder.pathFromRoot(dir));
1366 }
1367
1368 for (self.cflags.toSliceConst()) |cflag| {
1369 %%args.append(cflag);
1370 }
1371 }
1372
1373 fn make(step: &Step) -> %void {
1374 const self = @fieldParentPtr(CLibExeObjStep, "step", step);
1375 const cc = os.getEnv("CC") ?? "cc";
1376 const builder = self.builder;
1377
1378 var cc_args = ArrayList([]const u8).init(builder.allocator);
1379 defer cc_args.deinit();
1380
1381 switch (self.kind) {
1382 Kind.Obj => {
1383 %%cc_args.append("-c");
1384 %%cc_args.append(builder.pathFromRoot(self.object_src));
1385
1386 const output_path = builder.pathFromRoot(self.getOutputPath());
1387 %%cc_args.append("-o");
1388 %%cc_args.append(output_path);
1389
1390 self.appendCompileFlags(&cc_args);
1391
1392 %return builder.spawnChild(cc, cc_args.toSliceConst());
1393 },
1394 Kind.Lib => {
1395 for (self.source_files.toSliceConst()) |source_file| {
1396 %%cc_args.resize(0);
1397
1398 if (!self.static) {
1399 %%cc_args.append("-fPIC");
1400 }
1401
1402 const abs_source_file = builder.pathFromRoot(source_file);
1403 %%cc_args.append("-c");
1404 %%cc_args.append(abs_source_file);
1405
1406 const cache_o_src = %%os.path.join(builder.allocator, builder.cache_root, source_file);
1407 const cache_o_dir = os.path.dirname(cache_o_src);
1408 %return builder.makePath(cache_o_dir);
1409 const cache_o_file = builder.fmt("{}{}", cache_o_src, self.target.oFileExt());
1410 %%cc_args.append("-o");
1411 %%cc_args.append(builder.pathFromRoot(cache_o_file));
1412
1413 self.appendCompileFlags(&cc_args);
1414
1415 %return builder.spawnChild(cc, cc_args.toSliceConst());
14161248
1417 %%self.object_files.append(cache_o_file);1249 %%self.object_files.append(cache_o_file);
1418 }1250 }
...@@ -1539,8 +1371,57 @@ pub const CLibExeObjStep = struct {...@@ -1539,8 +1371,57 @@ pub const CLibExeObjStep = struct {
1539 },1371 },
1540 }1372 }
1541 }1373 }
1374};
1375
1376pub const TestStep = struct {
1377 step: Step,
1378 builder: &Builder,
1379 root_src: []const u8,
1380 build_mode: builtin.Mode,
1381 verbose: bool,
1382 link_libs: BufSet,
1383 name_prefix: []const u8,
1384 filter: ?[]const u8,
1385 target: Target,
1386 exec_cmd_args: ?[]const ?[]const u8,
1387
1388 pub fn init(builder: &Builder, root_src: []const u8) -> TestStep {
1389 const step_name = builder.fmt("test {}", root_src);
1390 TestStep {
1391 .step = Step.init(step_name, builder.allocator, make),
1392 .builder = builder,
1393 .root_src = root_src,
1394 .build_mode = builtin.Mode.Debug,
1395 .verbose = false,
1396 .name_prefix = "",
1397 .filter = null,
1398 .link_libs = BufSet.init(builder.allocator),
1399 .target = Target.Native,
1400 .exec_cmd_args = null,
1401 }
1402 }
15421403
1543 pub fn setTarget(self: &CLibExeObjStep, target_arch: builtin.Arch, target_os: builtin.Os,1404 pub fn setVerbose(self: &TestStep, value: bool) {
1405 self.verbose = value;
1406 }
1407
1408 pub fn setBuildMode(self: &TestStep, mode: builtin.Mode) {
1409 self.build_mode = mode;
1410 }
1411
1412 pub fn linkSystemLibrary(self: &TestStep, name: []const u8) {
1413 %%self.link_libs.put(name);
1414 }
1415
1416 pub fn setNamePrefix(self: &TestStep, text: []const u8) {
1417 self.name_prefix = text;
1418 }
1419
1420 pub fn setFilter(self: &TestStep, text: ?[]const u8) {
1421 self.filter = text;
1422 }
1423
1424 pub fn setTarget(self: &TestStep, target_arch: builtin.Arch, target_os: builtin.Os,
1544 target_environ: builtin.Environ)1425 target_environ: builtin.Environ)
1545 {1426 {
1546 self.target = Target.Cross {1427 self.target = Target.Cross {
...@@ -1551,6 +1432,92 @@ pub const CLibExeObjStep = struct {...@@ -1551,6 +1432,92 @@ pub const CLibExeObjStep = struct {
1551 }1432 }
1552 };1433 };
1553 }1434 }
1435
1436 pub fn setExecCmd(self: &TestStep, args: []const ?[]const u8) {
1437 self.exec_cmd_args = args;
1438 }
1439
1440 fn make(step: &Step) -> %void {
1441 const self = @fieldParentPtr(TestStep, "step", step);
1442 const builder = self.builder;
1443
1444 var zig_args = ArrayList([]const u8).init(builder.allocator);
1445 defer zig_args.deinit();
1446
1447 %%zig_args.append("test");
1448 %%zig_args.append(builder.pathFromRoot(self.root_src));
1449
1450 if (self.verbose) {
1451 %%zig_args.append("--verbose");
1452 }
1453
1454 switch (self.build_mode) {
1455 builtin.Mode.Debug => {},
1456 builtin.Mode.ReleaseSafe => %%zig_args.append("--release-safe"),
1457 builtin.Mode.ReleaseFast => %%zig_args.append("--release-fast"),
1458 }
1459
1460 switch (self.target) {
1461 Target.Native => {},
1462 Target.Cross => |cross_target| {
1463 %%zig_args.append("--target-arch");
1464 %%zig_args.append(@enumTagName(cross_target.arch));
1465
1466 %%zig_args.append("--target-os");
1467 %%zig_args.append(@enumTagName(cross_target.os));
1468
1469 %%zig_args.append("--target-environ");
1470 %%zig_args.append(@enumTagName(cross_target.environ));
1471 },
1472 }
1473
1474 if (self.filter) |filter| {
1475 %%zig_args.append("--test-filter");
1476 %%zig_args.append(filter);
1477 }
1478
1479 if (self.name_prefix.len != 0) {
1480 %%zig_args.append("--test-name-prefix");
1481 %%zig_args.append(self.name_prefix);
1482 }
1483
1484 {
1485 var it = self.link_libs.iterator();
1486 while (true) {
1487 const entry = it.next() ?? break;
1488 %%zig_args.append("--library");
1489 %%zig_args.append(entry.key);
1490 }
1491 }
1492
1493 if (self.exec_cmd_args) |exec_cmd_args| {
1494 for (exec_cmd_args) |cmd_arg| {
1495 if (cmd_arg) |arg| {
1496 %%zig_args.append("--test-cmd");
1497 %%zig_args.append(arg);
1498 } else {
1499 %%zig_args.append("--test-cmd-bin");
1500 }
1501 }
1502 }
1503
1504 for (builder.include_paths.toSliceConst()) |include_path| {
1505 %%zig_args.append("-isystem");
1506 %%zig_args.append(builder.pathFromRoot(include_path));
1507 }
1508
1509 for (builder.rpaths.toSliceConst()) |rpath| {
1510 %%zig_args.append("-rpath");
1511 %%zig_args.append(rpath);
1512 }
1513
1514 for (builder.lib_paths.toSliceConst()) |lib_path| {
1515 %%zig_args.append("--library-path");
1516 %%zig_args.append(lib_path);
1517 }
1518
1519 %return builder.spawnChild(builder.zig_exe, zig_args.toSliceConst());
1520 }
1554};1521};
15551522
1556pub const CommandStep = struct {1523pub const CommandStep = struct {
...@@ -1586,56 +1553,54 @@ pub const CommandStep = struct {...@@ -1586,56 +1553,54 @@ pub const CommandStep = struct {
1586 }1553 }
1587};1554};
15881555
1589fn InstallArtifactStep(comptime Artifact: type) -> type {1556const InstallArtifactStep = struct {
1590 struct {1557 step: Step,
1591 step: Step,1558 builder: &Builder,
1592 builder: &Builder,1559 artifact: &LibExeObjStep,
1593 artifact: &Artifact,1560 dest_file: []const u8,
1594 dest_file: []const u8,1561
15951562 const Self = this;
1596 const Self = this;1563
15971564 pub fn create(builder: &Builder, artifact: &LibExeObjStep) -> &Self {
1598 pub fn create(builder: &Builder, artifact: &Artifact) -> &Self {1565 const self = %%builder.allocator.create(Self);
1599 const self = %%builder.allocator.create(Self);1566 const dest_dir = switch (artifact.kind) {
1600 const dest_dir = switch (artifact.kind) {1567 LibExeObjStep.Kind.Obj => unreachable,
1601 Artifact.Kind.Obj => unreachable,1568 LibExeObjStep.Kind.Exe => builder.exe_dir,
1602 Artifact.Kind.Exe => builder.exe_dir,1569 LibExeObjStep.Kind.Lib => builder.lib_dir,
1603 Artifact.Kind.Lib => builder.lib_dir,1570 };
1604 };1571 *self = Self {
1605 *self = Self {1572 .builder = builder,
1606 .builder = builder,1573 .step = Step.init(builder.fmt("install {}", artifact.step.name), builder.allocator, make),
1607 .step = Step.init(builder.fmt("install {}", artifact.step.name), builder.allocator, make),1574 .artifact = artifact,
1608 .artifact = artifact,1575 .dest_file = %%os.path.join(builder.allocator, dest_dir, artifact.out_filename),
1609 .dest_file = %%os.path.join(builder.allocator, dest_dir, artifact.out_filename),1576 };
1610 };1577 self.step.dependOn(&artifact.step);
1611 self.step.dependOn(&artifact.step);1578 builder.pushInstalledFile(self.dest_file);
1612 builder.pushInstalledFile(self.dest_file);1579 if (self.artifact.kind == LibExeObjStep.Kind.Lib and !self.artifact.static) {
1613 if (self.artifact.kind == Artifact.Kind.Lib and !self.artifact.static) {1580 builder.pushInstalledFile(%%os.path.join(builder.allocator, builder.lib_dir,
1614 builder.pushInstalledFile(%%os.path.join(builder.allocator, builder.lib_dir,1581 artifact.major_only_filename));
1615 artifact.major_only_filename));1582 builder.pushInstalledFile(%%os.path.join(builder.allocator, builder.lib_dir,
1616 builder.pushInstalledFile(%%os.path.join(builder.allocator, builder.lib_dir,1583 artifact.name_only_filename));
1617 artifact.name_only_filename));
1618 }
1619 return self;
1620 }1584 }
1585 return self;
1586 }
16211587
1622 fn make(step: &Step) -> %void {1588 fn make(step: &Step) -> %void {
1623 const self = @fieldParentPtr(Self, "step", step);1589 const self = @fieldParentPtr(Self, "step", step);
1624 const builder = self.builder;1590 const builder = self.builder;
16251591
1626 const mode = switch (self.artifact.kind) {1592 const mode = switch (self.artifact.kind) {
1627 Artifact.Kind.Obj => unreachable,1593 LibExeObjStep.Kind.Obj => unreachable,
1628 Artifact.Kind.Exe => usize(0o755),1594 LibExeObjStep.Kind.Exe => usize(0o755),
1629 Artifact.Kind.Lib => if (self.artifact.static) usize(0o666) else usize(0o755),1595 LibExeObjStep.Kind.Lib => if (self.artifact.static) usize(0o666) else usize(0o755),
1630 };1596 };
1631 %return builder.copyFileMode(self.artifact.getOutputPath(), self.dest_file, mode);1597 %return builder.copyFileMode(self.artifact.getOutputPath(), self.dest_file, mode);
1632 if (self.artifact.kind == Artifact.Kind.Lib and !self.artifact.static) {1598 if (self.artifact.kind == LibExeObjStep.Kind.Lib and !self.artifact.static) {
1633 %return doAtomicSymLinks(builder.allocator, self.dest_file,1599 %return doAtomicSymLinks(builder.allocator, self.dest_file,
1634 self.artifact.major_only_filename, self.artifact.name_only_filename);1600 self.artifact.major_only_filename, self.artifact.name_only_filename);
1635 }
1636 }1601 }
1637 }1602 }
1638}1603};
16391604
1640pub const InstallFileStep = struct {1605pub const InstallFileStep = struct {
1641 step: Step,1606 step: Step,