| ... | ... | @@ -151,22 +151,20 @@ pub const Builder = struct { |
| 151 | 151 | return obj_step; |
| 152 | 152 | } |
| 153 | 153 | |
| 154 | | pub fn addCStaticLibrary(self: &Builder, name: []const u8) -> &CLibrary { |
| 155 | | const lib = %%self.allocator.create(CLibrary); |
| 156 | | *lib = CLibrary.initStatic(self, name); |
| 157 | | return lib; |
| 154 | pub fn addCStaticLibrary(self: &Builder, name: []const u8) -> &CLibExeObjStep { |
| 155 | return CLibExeObjStep.createStaticLibrary(self, name); |
| 158 | 156 | } |
| 159 | 157 | |
| 160 | | pub fn addCSharedLibrary(self: &Builder, name: []const u8, ver: &const Version) -> &CLibrary { |
| 161 | | const lib = %%self.allocator.create(CLibrary); |
| 162 | | *lib = CLibrary.initShared(self, name, ver); |
| 163 | | return lib; |
| 158 | pub fn addCSharedLibrary(self: &Builder, name: []const u8, ver: &const Version) -> &CLibExeObjStep { |
| 159 | return CLibExeObjStep.createSharedLibrary(self, name, ver); |
| 164 | 160 | } |
| 165 | 161 | |
| 166 | | pub fn addCExecutable(self: &Builder, name: []const u8) -> &CExecutable { |
| 167 | | const exe = %%self.allocator.create(CExecutable); |
| 168 | | *exe = CExecutable.init(self, name); |
| 169 | | return exe; |
| 162 | pub fn addCExecutable(self: &Builder, name: []const u8) -> &CLibExeObjStep { |
| 163 | return CLibExeObjStep.createExecutable(self, name); |
| 164 | } |
| 165 | |
| 166 | pub fn addCObject(self: &Builder, name: []const u8, src: []const u8) -> &CLibExeObjStep { |
| 167 | return CLibExeObjStep.createObject(self, name, src); |
| 170 | 168 | } |
| 171 | 169 | |
| 172 | 170 | pub fn addCommand(self: &Builder, cwd: ?[]const u8, env_map: &const BufMap, |
| ... | ... | @@ -533,7 +531,7 @@ pub const Builder = struct { |
| 533 | 531 | }; |
| 534 | 532 | } |
| 535 | 533 | |
| 536 | | pub fn installCLibrary(self: &Builder, lib: &CLibrary) -> &InstallCLibraryStep { |
| 534 | pub fn installCLibrary(self: &Builder, lib: &CLibExeObjStep) -> &InstallCLibraryStep { |
| 537 | 535 | const install_step = %%self.allocator.create(InstallCLibraryStep); |
| 538 | 536 | *install_step = InstallCLibraryStep.init(self, lib); |
| 539 | 537 | install_step.step.dependOn(&lib.step); |
| ... | ... | @@ -1007,8 +1005,7 @@ pub const TestStep = struct { |
| 1007 | 1005 | } |
| 1008 | 1006 | }; |
| 1009 | 1007 | |
| 1010 | | // TODO merge with CExecutable |
| 1011 | | pub const CLibrary = struct { |
| 1008 | pub const CLibExeObjStep = struct { |
| 1012 | 1009 | step: Step, |
| 1013 | 1010 | name: []const u8, |
| 1014 | 1011 | out_filename: []const u8, |
| ... | ... | @@ -1019,24 +1016,51 @@ pub const CLibrary = struct { |
| 1019 | 1016 | source_files: List([]const u8), |
| 1020 | 1017 | object_files: List([]const u8), |
| 1021 | 1018 | link_libs: BufSet, |
| 1019 | full_path_libs: List([]const u8), |
| 1022 | 1020 | target: Target, |
| 1023 | 1021 | builder: &Builder, |
| 1024 | 1022 | include_dirs: List([]const u8), |
| 1025 | 1023 | major_only_filename: []const u8, |
| 1026 | 1024 | name_only_filename: []const u8, |
| 1025 | object_src: []const u8, |
| 1026 | kind: Kind, |
| 1027 | 1027 | |
| 1028 | | pub fn initShared(builder: &Builder, name: []const u8, version: &const Version) -> CLibrary { |
| 1029 | | return init(builder, name, version, false); |
| 1028 | const Kind = enum { |
| 1029 | Exe, |
| 1030 | Lib, |
| 1031 | Obj, |
| 1032 | }; |
| 1033 | |
| 1034 | pub fn createSharedLibrary(builder: &Builder, name: []const u8, version: &const Version) -> &CLibExeObjStep { |
| 1035 | const self = %%builder.allocator.create(CLibExeObjStep); |
| 1036 | *self = init(builder, name, Kind.Lib, builder.version(0, 0, 0), false); |
| 1037 | return self; |
| 1030 | 1038 | } |
| 1031 | 1039 | |
| 1032 | | pub fn initStatic(builder: &Builder, name: []const u8) -> CLibrary { |
| 1033 | | return init(builder, name, builder.version(0, 0, 0), true); |
| 1040 | pub fn createStaticLibrary(builder: &Builder, name: []const u8) -> &CLibExeObjStep { |
| 1041 | const self = %%builder.allocator.create(CLibExeObjStep); |
| 1042 | *self = init(builder, name, Kind.Lib, builder.version(0, 0, 0), true); |
| 1043 | return self; |
| 1034 | 1044 | } |
| 1035 | 1045 | |
| 1036 | | fn init(builder: &Builder, name: []const u8, version: &const Version, static: bool) -> CLibrary { |
| 1037 | | var clib = CLibrary { |
| 1046 | pub fn createObject(builder: &Builder, name: []const u8, src: []const u8) -> &CLibExeObjStep { |
| 1047 | const self = %%builder.allocator.create(CLibExeObjStep); |
| 1048 | *self = init(builder, name, Kind.Obj, builder.version(0, 0, 0), false); |
| 1049 | self.object_src = src; |
| 1050 | return self; |
| 1051 | } |
| 1052 | |
| 1053 | pub fn createExecutable(builder: &Builder, name: []const u8) -> &CLibExeObjStep { |
| 1054 | const self = %%builder.allocator.create(CLibExeObjStep); |
| 1055 | *self = init(builder, name, Kind.Exe, builder.version(0, 0, 0), false); |
| 1056 | return self; |
| 1057 | } |
| 1058 | |
| 1059 | fn init(builder: &Builder, name: []const u8, kind: Kind, version: &const Version, static: bool) -> CLibExeObjStep { |
| 1060 | var clib = CLibExeObjStep { |
| 1038 | 1061 | .builder = builder, |
| 1039 | 1062 | .name = name, |
| 1063 | .kind = kind, |
| 1040 | 1064 | .version = *version, |
| 1041 | 1065 | .static = static, |
| 1042 | 1066 | .target = Target.Native, |
| ... | ... | @@ -1045,32 +1069,44 @@ pub const CLibrary = struct { |
| 1045 | 1069 | .object_files = List([]const u8).init(builder.allocator), |
| 1046 | 1070 | .step = Step.init(name, builder.allocator, make), |
| 1047 | 1071 | .link_libs = BufSet.init(builder.allocator), |
| 1072 | .full_path_libs = List([]const u8).init(builder.allocator), |
| 1048 | 1073 | .include_dirs = List([]const u8).init(builder.allocator), |
| 1049 | 1074 | .output_path = null, |
| 1050 | 1075 | .out_filename = undefined, |
| 1051 | 1076 | .major_only_filename = undefined, |
| 1052 | 1077 | .name_only_filename = undefined, |
| 1078 | .object_src = undefined, |
| 1053 | 1079 | }; |
| 1054 | | clib.computeOutFileName(); |
| 1080 | clib.computeOutFileNames(); |
| 1055 | 1081 | return clib; |
| 1056 | 1082 | } |
| 1057 | 1083 | |
| 1058 | | fn computeOutFileName(self: &CLibrary) { |
| 1059 | | if (self.static) { |
| 1060 | | self.out_filename = self.builder.fmt("lib{}.a", self.name); |
| 1061 | | } else { |
| 1062 | | self.out_filename = self.builder.fmt("lib{}.so.{d}.{d}.{d}", |
| 1063 | | self.name, self.version.major, self.version.minor, self.version.patch); |
| 1064 | | self.major_only_filename = self.builder.fmt("lib{}.so.{d}", self.name, self.version.major); |
| 1065 | | self.name_only_filename = self.builder.fmt("lib{}.so", self.name); |
| 1084 | fn computeOutFileNames(self: &CLibExeObjStep) { |
| 1085 | switch (self.kind) { |
| 1086 | Kind.Obj => { |
| 1087 | self.out_filename = self.builder.fmt("{}{}", self.name, self.target.oFileExt()); |
| 1088 | }, |
| 1089 | Kind.Exe => { |
| 1090 | self.out_filename = self.builder.fmt("{}{}", self.name, self.target.exeFileExt()); |
| 1091 | }, |
| 1092 | Kind.Lib => { |
| 1093 | if (self.static) { |
| 1094 | self.out_filename = self.builder.fmt("lib{}.a", self.name); |
| 1095 | } else { |
| 1096 | self.out_filename = self.builder.fmt("lib{}.so.{d}.{d}.{d}", |
| 1097 | self.name, self.version.major, self.version.minor, self.version.patch); |
| 1098 | self.major_only_filename = self.builder.fmt("lib{}.so.{d}", self.name, self.version.major); |
| 1099 | self.name_only_filename = self.builder.fmt("lib{}.so", self.name); |
| 1100 | } |
| 1101 | }, |
| 1066 | 1102 | } |
| 1067 | 1103 | } |
| 1068 | 1104 | |
| 1069 | | pub fn setOutputPath(self: &CLibrary, value: []const u8) { |
| 1105 | pub fn setOutputPath(self: &CLibExeObjStep, value: []const u8) { |
| 1070 | 1106 | self.output_path = value; |
| 1071 | 1107 | } |
| 1072 | 1108 | |
| 1073 | | pub fn getOutputPath(self: &CLibrary) -> []const u8 { |
| 1109 | pub fn getOutputPath(self: &CLibExeObjStep) -> []const u8 { |
| 1074 | 1110 | test (self.output_path) |output_path| { |
| 1075 | 1111 | output_path |
| 1076 | 1112 | } else { |
| ... | ... | @@ -1079,28 +1115,54 @@ pub const CLibrary = struct { |
| 1079 | 1115 | } |
| 1080 | 1116 | } |
| 1081 | 1117 | |
| 1082 | | pub fn linkSystemLibrary(self: &CLibrary, name: []const u8) { |
| 1118 | pub fn linkLibrary(self: &CLibExeObjStep, lib: &LibExeObjStep) { |
| 1119 | assert(self.kind != Kind.Obj); |
| 1120 | assert(lib.kind == LibExeObjStep.Kind.Lib); |
| 1121 | self.step.dependOn(&lib.step); |
| 1122 | %%self.full_path_libs.append(lib.getOutputPath()); |
| 1123 | // TODO should be some kind of isolated directory that only has this header in it |
| 1124 | %%self.include_dirs.append(self.builder.cache_root); |
| 1125 | } |
| 1126 | |
| 1127 | pub fn linkSystemLibrary(self: &CLibExeObjStep, name: []const u8) { |
| 1128 | assert(self.kind != Kind.Obj); |
| 1083 | 1129 | %%self.link_libs.put(name); |
| 1084 | 1130 | } |
| 1085 | 1131 | |
| 1086 | | pub fn linkCLibrary(self: &CLibrary, other: &CLibrary) { |
| 1132 | pub fn linkCLibrary(self: &CLibExeObjStep, other: &CLibExeObjStep) { |
| 1133 | assert(self.kind != Kind.Obj); |
| 1134 | assert(other.kind == Kind.Lib); |
| 1087 | 1135 | self.step.dependOn(&other.step); |
| 1088 | | %%self.link_libs.put(other.name); |
| 1136 | %%self.full_path_libs.append(other.getOutputPath()); |
| 1089 | 1137 | } |
| 1090 | 1138 | |
| 1091 | | pub fn addSourceFile(self: &CLibrary, file: []const u8) { |
| 1139 | pub fn addSourceFile(self: &CLibExeObjStep, file: []const u8) { |
| 1140 | assert(self.kind != Kind.Obj); |
| 1092 | 1141 | %%self.source_files.append(file); |
| 1093 | 1142 | } |
| 1094 | 1143 | |
| 1095 | | pub fn addObjectFile(self: &CLibrary, file: []const u8) { |
| 1144 | pub fn addObjectFile(self: &CLibExeObjStep, file: []const u8) { |
| 1145 | assert(self.kind != Kind.Obj); |
| 1096 | 1146 | %%self.object_files.append(file); |
| 1097 | 1147 | } |
| 1098 | 1148 | |
| 1099 | | pub fn addIncludeDir(self: &CLibrary, path: []const u8) { |
| 1149 | pub fn addObject(self: &CLibExeObjStep, obj: &LibExeObjStep) { |
| 1150 | assert(self.kind != Kind.Obj); |
| 1151 | assert(obj.kind == LibExeObjStep.Kind.Obj); |
| 1152 | |
| 1153 | self.step.dependOn(&obj.step); |
| 1154 | |
| 1155 | %%self.object_files.append(obj.getOutputPath()); |
| 1156 | |
| 1157 | // TODO should be some kind of isolated directory that only has this header in it |
| 1158 | %%self.include_dirs.append(self.builder.cache_root); |
| 1159 | } |
| 1160 | |
| 1161 | pub fn addIncludeDir(self: &CLibExeObjStep, path: []const u8) { |
| 1100 | 1162 | %%self.include_dirs.append(path); |
| 1101 | 1163 | } |
| 1102 | 1164 | |
| 1103 | | pub fn addCompileFlagsForRelease(self: &CLibrary, release: bool) { |
| 1165 | pub fn addCompileFlagsForRelease(self: &CLibExeObjStep, release: bool) { |
| 1104 | 1166 | if (release) { |
| 1105 | 1167 | %%self.cflags.append("-g"); |
| 1106 | 1168 | %%self.cflags.append("-O2"); |
| ... | ... | @@ -1109,256 +1171,164 @@ pub const CLibrary = struct { |
| 1109 | 1171 | } |
| 1110 | 1172 | } |
| 1111 | 1173 | |
| 1112 | | pub fn addCompileFlags(self: &CLibrary, flags: []const []const u8) { |
| 1174 | pub fn addCompileFlags(self: &CLibExeObjStep, flags: []const []const u8) { |
| 1113 | 1175 | for (flags) |flag| { |
| 1114 | 1176 | %%self.cflags.append(flag); |
| 1115 | 1177 | } |
| 1116 | 1178 | } |
| 1117 | 1179 | |
| 1118 | 1180 | fn make(step: &Step) -> %void { |
| 1119 | | const self = @fieldParentPtr(CLibrary, "step", step); |
| 1181 | const self = @fieldParentPtr(CLibExeObjStep, "step", step); |
| 1120 | 1182 | const cc = os.getEnv("CC") ?? "cc"; |
| 1121 | 1183 | const builder = self.builder; |
| 1122 | 1184 | |
| 1123 | 1185 | var cc_args = List([]const u8).init(builder.allocator); |
| 1124 | 1186 | defer cc_args.deinit(); |
| 1125 | 1187 | |
| 1126 | | for (self.source_files.toSliceConst()) |source_file| { |
| 1127 | | %%cc_args.resize(0); |
| 1128 | | |
| 1129 | | if (!self.static) { |
| 1130 | | %%cc_args.append("-fPIC"); |
| 1131 | | } |
| 1132 | | |
| 1133 | | %%cc_args.append("-c"); |
| 1134 | | %%cc_args.append(source_file); |
| 1135 | | |
| 1136 | | const rel_src_path = %%os.path.relative(builder.allocator, builder.build_root, source_file); |
| 1137 | | const cache_o_src = %%os.path.join(builder.allocator, builder.cache_root, rel_src_path); |
| 1138 | | const cache_o_dir = os.path.dirname(cache_o_src); |
| 1139 | | %return builder.makePath(cache_o_dir); |
| 1140 | | const cache_o_file = builder.fmt("{}{}", cache_o_src, self.target.oFileExt()); |
| 1141 | | %%cc_args.append("-o"); |
| 1142 | | %%cc_args.append(cache_o_file); |
| 1143 | | |
| 1144 | | for (self.cflags.toSliceConst()) |cflag| { |
| 1145 | | %%cc_args.append(cflag); |
| 1146 | | } |
| 1147 | | |
| 1148 | | for (self.include_dirs.toSliceConst()) |dir| { |
| 1149 | | %%cc_args.append("-I"); |
| 1150 | | %%cc_args.append(dir); |
| 1151 | | } |
| 1152 | | |
| 1153 | | %return builder.spawnChild(cc, cc_args.toSliceConst()); |
| 1154 | | |
| 1155 | | %%self.object_files.append(cache_o_file); |
| 1156 | | } |
| 1157 | | |
| 1158 | | if (self.static) { |
| 1159 | | debug.panic("TODO static library"); |
| 1160 | | } else { |
| 1161 | | %%cc_args.resize(0); |
| 1162 | | |
| 1163 | | %%cc_args.append("-fPIC"); |
| 1164 | | %%cc_args.append("-shared"); |
| 1165 | | |
| 1166 | | const soname_arg = builder.fmt("-Wl,-soname,lib{}.so.{d}", self.name, self.version.major); |
| 1167 | | defer builder.allocator.free(soname_arg); |
| 1168 | | %%cc_args.append(soname_arg); |
| 1169 | | |
| 1170 | | const output_path = builder.pathFromRoot(self.getOutputPath()); |
| 1171 | | %%cc_args.append("-o"); |
| 1172 | | %%cc_args.append(output_path); |
| 1173 | | |
| 1174 | | for (self.object_files.toSliceConst()) |object_file| { |
| 1175 | | %%cc_args.append(builder.pathFromRoot(object_file)); |
| 1176 | | } |
| 1177 | | |
| 1178 | | %return builder.spawnChild(cc, cc_args.toSliceConst()); |
| 1179 | | |
| 1180 | | %return doAtomicSymLinks(builder.allocator, output_path, self.major_only_filename, |
| 1181 | | self.name_only_filename); |
| 1182 | | } |
| 1183 | | } |
| 1184 | | |
| 1185 | | pub fn setTarget(self: &CLibrary, target_arch: Arch, target_os: Os, target_environ: Environ) { |
| 1186 | | self.target = Target.Cross { |
| 1187 | | CrossTarget { |
| 1188 | | .arch = target_arch, |
| 1189 | | .os = target_os, |
| 1190 | | .environ = target_environ, |
| 1191 | | } |
| 1192 | | }; |
| 1193 | | } |
| 1194 | | }; |
| 1195 | | |
| 1196 | | pub const CExecutable = struct { |
| 1197 | | step: Step, |
| 1198 | | builder: &Builder, |
| 1199 | | name: []const u8, |
| 1200 | | cflags: List([]const u8), |
| 1201 | | source_files: List([]const u8), |
| 1202 | | object_files: List([]const u8), |
| 1203 | | full_path_libs: List([]const u8), |
| 1204 | | link_libs: BufSet, |
| 1205 | | target: Target, |
| 1206 | | include_dirs: List([]const u8), |
| 1207 | | output_path: ?[]const u8, |
| 1208 | | out_filename: []const u8, |
| 1209 | | |
| 1210 | | pub fn init(builder: &Builder, name: []const u8) -> CExecutable { |
| 1211 | | var self = CExecutable { |
| 1212 | | .builder = builder, |
| 1213 | | .name = name, |
| 1214 | | .target = Target.Native, |
| 1215 | | .cflags = List([]const u8).init(builder.allocator), |
| 1216 | | .source_files = List([]const u8).init(builder.allocator), |
| 1217 | | .object_files = List([]const u8).init(builder.allocator), |
| 1218 | | .full_path_libs = List([]const u8).init(builder.allocator), |
| 1219 | | .step = Step.init(name, builder.allocator, make), |
| 1220 | | .link_libs = BufSet.init(builder.allocator), |
| 1221 | | .include_dirs = List([]const u8).init(builder.allocator), |
| 1222 | | .output_path = null, |
| 1223 | | .out_filename = undefined, |
| 1224 | | }; |
| 1225 | | self.computeOutFileName(); |
| 1226 | | return self; |
| 1227 | | } |
| 1188 | switch (self.kind) { |
| 1189 | Kind.Obj => { |
| 1190 | %%cc_args.append("-c"); |
| 1191 | %%cc_args.append(builder.pathFromRoot(self.object_src)); |
| 1228 | 1192 | |
| 1229 | | fn computeOutFileName(self: &CExecutable) { |
| 1230 | | self.out_filename = self.builder.fmt("{}{}", self.name, self.target.exeFileExt()); |
| 1231 | | } |
| 1193 | const output_path = builder.pathFromRoot(self.getOutputPath()); |
| 1194 | %%cc_args.append("-o"); |
| 1195 | %%cc_args.append(output_path); |
| 1232 | 1196 | |
| 1233 | | pub fn setOutputPath(self: &CExecutable, value: []const u8) { |
| 1234 | | self.output_path = value; |
| 1235 | | } |
| 1197 | for (self.cflags.toSliceConst()) |cflag| { |
| 1198 | %%cc_args.append(cflag); |
| 1199 | } |
| 1236 | 1200 | |
| 1237 | | pub fn getOutputPath(self: &CExecutable) -> []const u8 { |
| 1238 | | test (self.output_path) |output_path| { |
| 1239 | | self.builder.pathFromRoot(output_path) |
| 1240 | | } else { |
| 1241 | | const wanted_path = %%os.path.join(self.builder.allocator, self.builder.cache_root, self.out_filename); |
| 1242 | | %%os.path.relative(self.builder.allocator, self.builder.build_root, wanted_path) |
| 1243 | | } |
| 1244 | | } |
| 1201 | for (self.include_dirs.toSliceConst()) |dir| { |
| 1202 | %%cc_args.append("-I"); |
| 1203 | %%cc_args.append(builder.pathFromRoot(dir)); |
| 1204 | } |
| 1245 | 1205 | |
| 1246 | | pub fn linkSystemLibrary(self: &CExecutable, name: []const u8) { |
| 1247 | | %%self.link_libs.put(name); |
| 1248 | | } |
| 1206 | %return builder.spawnChild(cc, cc_args.toSliceConst()); |
| 1207 | }, |
| 1208 | Kind.Lib => { |
| 1209 | for (self.source_files.toSliceConst()) |source_file| { |
| 1210 | %%cc_args.resize(0); |
| 1249 | 1211 | |
| 1250 | | pub fn linkCLibrary(self: &CExecutable, clib: &CLibrary) { |
| 1251 | | self.step.dependOn(&clib.step); |
| 1252 | | %%self.full_path_libs.append(clib.getOutputPath()); |
| 1253 | | } |
| 1212 | if (!self.static) { |
| 1213 | %%cc_args.append("-fPIC"); |
| 1214 | } |
| 1254 | 1215 | |
| 1255 | | pub fn linkLibrary(self: &CExecutable, lib: &LibExeObjStep) { |
| 1256 | | assert(lib.kind == LibExeObjStep.Kind.Lib); |
| 1257 | | self.step.dependOn(&lib.step); |
| 1258 | | %%self.full_path_libs.append(lib.getOutputPath()); |
| 1259 | | // TODO should be some kind of isolated directory that only has this header in it |
| 1260 | | %%self.include_dirs.append(self.builder.cache_root); |
| 1261 | | } |
| 1216 | %%cc_args.append("-c"); |
| 1217 | %%cc_args.append(source_file); |
| 1262 | 1218 | |
| 1263 | | pub fn addObject(self: &CExecutable, obj: &LibExeObjStep) { |
| 1264 | | assert(obj.kind == LibExeObjStep.Kind.Obj); |
| 1219 | const rel_src_path = %%os.path.relative(builder.allocator, builder.build_root, source_file); |
| 1220 | const cache_o_src = %%os.path.join(builder.allocator, builder.cache_root, rel_src_path); |
| 1221 | const cache_o_dir = os.path.dirname(cache_o_src); |
| 1222 | %return builder.makePath(cache_o_dir); |
| 1223 | const cache_o_file = builder.fmt("{}{}", cache_o_src, self.target.oFileExt()); |
| 1224 | %%cc_args.append("-o"); |
| 1225 | %%cc_args.append(cache_o_file); |
| 1265 | 1226 | |
| 1266 | | self.step.dependOn(&obj.step); |
| 1227 | for (self.cflags.toSliceConst()) |cflag| { |
| 1228 | %%cc_args.append(cflag); |
| 1229 | } |
| 1267 | 1230 | |
| 1268 | | %%self.object_files.append(obj.getOutputPath()); |
| 1231 | for (self.include_dirs.toSliceConst()) |dir| { |
| 1232 | %%cc_args.append("-I"); |
| 1233 | %%cc_args.append(dir); |
| 1234 | } |
| 1269 | 1235 | |
| 1270 | | // TODO should be some kind of isolated directory that only has this header in it |
| 1271 | | %%self.include_dirs.append(self.builder.cache_root); |
| 1272 | | } |
| 1236 | %return builder.spawnChild(cc, cc_args.toSliceConst()); |
| 1273 | 1237 | |
| 1274 | | pub fn addSourceFile(self: &CExecutable, file: []const u8) { |
| 1275 | | %%self.source_files.append(file); |
| 1276 | | } |
| 1238 | %%self.object_files.append(cache_o_file); |
| 1239 | } |
| 1277 | 1240 | |
| 1278 | | pub fn addObjectFile(self: &CExecutable, file: []const u8) { |
| 1279 | | %%self.object_files.append(file); |
| 1280 | | } |
| 1241 | if (self.static) { |
| 1242 | debug.panic("TODO static library"); |
| 1243 | } else { |
| 1244 | %%cc_args.resize(0); |
| 1281 | 1245 | |
| 1282 | | pub fn addIncludeDir(self: &CExecutable, path: []const u8) { |
| 1283 | | %%self.include_dirs.append(path); |
| 1284 | | } |
| 1246 | %%cc_args.append("-fPIC"); |
| 1247 | %%cc_args.append("-shared"); |
| 1285 | 1248 | |
| 1286 | | pub fn addCompileFlagsForRelease(self: &CExecutable, release: bool) { |
| 1287 | | if (release) { |
| 1288 | | %%self.cflags.append("-g"); |
| 1289 | | %%self.cflags.append("-O2"); |
| 1290 | | } else { |
| 1291 | | %%self.cflags.append("-g"); |
| 1292 | | } |
| 1293 | | } |
| 1249 | const soname_arg = builder.fmt("-Wl,-soname,lib{}.so.{d}", self.name, self.version.major); |
| 1250 | defer builder.allocator.free(soname_arg); |
| 1251 | %%cc_args.append(soname_arg); |
| 1294 | 1252 | |
| 1295 | | pub fn addCompileFlags(self: &CExecutable, flags: []const []const u8) { |
| 1296 | | for (flags) |flag| { |
| 1297 | | %%self.cflags.append(flag); |
| 1298 | | } |
| 1299 | | } |
| 1253 | const output_path = builder.pathFromRoot(self.getOutputPath()); |
| 1254 | %%cc_args.append("-o"); |
| 1255 | %%cc_args.append(output_path); |
| 1300 | 1256 | |
| 1301 | | fn make(step: &Step) -> %void { |
| 1302 | | const self = @fieldParentPtr(CExecutable, "step", step); |
| 1303 | | const cc = os.getEnv("CC") ?? "cc"; |
| 1304 | | const builder = self.builder; |
| 1257 | for (self.object_files.toSliceConst()) |object_file| { |
| 1258 | %%cc_args.append(builder.pathFromRoot(object_file)); |
| 1259 | } |
| 1305 | 1260 | |
| 1306 | | var cc_args = List([]const u8).init(builder.allocator); |
| 1307 | | defer cc_args.deinit(); |
| 1261 | const rpath_arg = builder.fmt("-Wl,-rpath,{}", |
| 1262 | %%os.path.real(builder.allocator, builder.cache_root)); |
| 1263 | defer builder.allocator.free(rpath_arg); |
| 1264 | %%cc_args.append(rpath_arg); |
| 1308 | 1265 | |
| 1309 | | for (self.source_files.toSliceConst()) |source_file| { |
| 1310 | | %%cc_args.resize(0); |
| 1266 | %%cc_args.append("-rdynamic"); |
| 1311 | 1267 | |
| 1312 | | %%cc_args.append("-c"); |
| 1313 | | %%cc_args.append(builder.pathFromRoot(source_file)); |
| 1268 | for (self.full_path_libs.toSliceConst()) |full_path_lib| { |
| 1269 | %%cc_args.append(builder.pathFromRoot(full_path_lib)); |
| 1270 | } |
| 1314 | 1271 | |
| 1315 | | const rel_src_path = %%os.path.relative(builder.allocator, builder.build_root, source_file); |
| 1316 | | const cache_o_src = %%os.path.join(builder.allocator, builder.cache_root, rel_src_path); |
| 1317 | | const cache_o_dir = os.path.dirname(cache_o_src); |
| 1318 | | %return builder.makePath(cache_o_dir); |
| 1319 | | const cache_o_file = builder.fmt("{}{}", cache_o_src, self.target.oFileExt()); |
| 1320 | | %%cc_args.append("-o"); |
| 1321 | | %%cc_args.append(cache_o_file); |
| 1272 | %return builder.spawnChild(cc, cc_args.toSliceConst()); |
| 1322 | 1273 | |
| 1323 | | for (self.cflags.toSliceConst()) |cflag| { |
| 1324 | | %%cc_args.append(cflag); |
| 1325 | | } |
| 1274 | %return doAtomicSymLinks(builder.allocator, output_path, self.major_only_filename, |
| 1275 | self.name_only_filename); |
| 1276 | } |
| 1277 | }, |
| 1278 | Kind.Exe => { |
| 1279 | for (self.source_files.toSliceConst()) |source_file| { |
| 1280 | %%cc_args.resize(0); |
| 1281 | |
| 1282 | %%cc_args.append("-c"); |
| 1283 | %%cc_args.append(builder.pathFromRoot(source_file)); |
| 1284 | |
| 1285 | const rel_src_path = %%os.path.relative(builder.allocator, builder.build_root, source_file); |
| 1286 | const cache_o_src = %%os.path.join(builder.allocator, builder.cache_root, rel_src_path); |
| 1287 | const cache_o_dir = os.path.dirname(cache_o_src); |
| 1288 | %return builder.makePath(cache_o_dir); |
| 1289 | const cache_o_file = builder.fmt("{}{}", cache_o_src, self.target.oFileExt()); |
| 1290 | %%cc_args.append("-o"); |
| 1291 | %%cc_args.append(cache_o_file); |
| 1292 | |
| 1293 | for (self.cflags.toSliceConst()) |cflag| { |
| 1294 | %%cc_args.append(cflag); |
| 1295 | } |
| 1326 | 1296 | |
| 1327 | | for (self.include_dirs.toSliceConst()) |dir| { |
| 1328 | | %%cc_args.append("-I"); |
| 1329 | | %%cc_args.append(builder.pathFromRoot(dir)); |
| 1330 | | } |
| 1297 | for (self.include_dirs.toSliceConst()) |dir| { |
| 1298 | %%cc_args.append("-I"); |
| 1299 | %%cc_args.append(builder.pathFromRoot(dir)); |
| 1300 | } |
| 1331 | 1301 | |
| 1332 | | %return builder.spawnChild(cc, cc_args.toSliceConst()); |
| 1302 | %return builder.spawnChild(cc, cc_args.toSliceConst()); |
| 1333 | 1303 | |
| 1334 | | %%self.object_files.append(%%os.path.relative(builder.allocator, builder.build_root, cache_o_file)); |
| 1335 | | } |
| 1304 | %%self.object_files.append(%%os.path.relative(builder.allocator, builder.build_root, cache_o_file)); |
| 1305 | } |
| 1336 | 1306 | |
| 1337 | | %%cc_args.resize(0); |
| 1307 | %%cc_args.resize(0); |
| 1338 | 1308 | |
| 1339 | | for (self.object_files.toSliceConst()) |object_file| { |
| 1340 | | %%cc_args.append(builder.pathFromRoot(object_file)); |
| 1341 | | } |
| 1309 | for (self.object_files.toSliceConst()) |object_file| { |
| 1310 | %%cc_args.append(builder.pathFromRoot(object_file)); |
| 1311 | } |
| 1342 | 1312 | |
| 1343 | | const output_path = builder.pathFromRoot(self.getOutputPath()); |
| 1344 | | %%cc_args.append("-o"); |
| 1345 | | %%cc_args.append(output_path); |
| 1313 | const output_path = builder.pathFromRoot(self.getOutputPath()); |
| 1314 | %%cc_args.append("-o"); |
| 1315 | %%cc_args.append(output_path); |
| 1346 | 1316 | |
| 1347 | | const rpath_arg = builder.fmt("-Wl,-rpath,{}", |
| 1348 | | %%os.path.real(builder.allocator, builder.cache_root)); |
| 1349 | | defer builder.allocator.free(rpath_arg); |
| 1350 | | %%cc_args.append(rpath_arg); |
| 1317 | const rpath_arg = builder.fmt("-Wl,-rpath,{}", |
| 1318 | %%os.path.real(builder.allocator, builder.cache_root)); |
| 1319 | defer builder.allocator.free(rpath_arg); |
| 1320 | %%cc_args.append(rpath_arg); |
| 1351 | 1321 | |
| 1352 | | %%cc_args.append("-rdynamic"); |
| 1322 | %%cc_args.append("-rdynamic"); |
| 1353 | 1323 | |
| 1354 | | for (self.full_path_libs.toSliceConst()) |full_path_lib| { |
| 1355 | | %%cc_args.append(builder.pathFromRoot(full_path_lib)); |
| 1324 | for (self.full_path_libs.toSliceConst()) |full_path_lib| { |
| 1325 | %%cc_args.append(builder.pathFromRoot(full_path_lib)); |
| 1326 | } |
| 1327 | }, |
| 1356 | 1328 | } |
| 1357 | | |
| 1358 | | %return builder.spawnChild(cc, cc_args.toSliceConst()); |
| 1359 | 1329 | } |
| 1360 | 1330 | |
| 1361 | | pub fn setTarget(self: &CExecutable, target_arch: Arch, target_os: Os, target_environ: Environ) { |
| 1331 | pub fn setTarget(self: &CLibExeObjStep, target_arch: Arch, target_os: Os, target_environ: Environ) { |
| 1362 | 1332 | self.target = Target.Cross { |
| 1363 | 1333 | CrossTarget { |
| 1364 | 1334 | .arch = target_arch, |
| ... | ... | @@ -1403,10 +1373,10 @@ pub const CommandStep = struct { |
| 1403 | 1373 | pub const InstallCLibraryStep = struct { |
| 1404 | 1374 | step: Step, |
| 1405 | 1375 | builder: &Builder, |
| 1406 | | lib: &CLibrary, |
| 1376 | lib: &CLibExeObjStep, |
| 1407 | 1377 | dest_file: []const u8, |
| 1408 | 1378 | |
| 1409 | | pub fn init(builder: &Builder, lib: &CLibrary) -> InstallCLibraryStep { |
| 1379 | pub fn init(builder: &Builder, lib: &CLibExeObjStep) -> InstallCLibraryStep { |
| 1410 | 1380 | var self = InstallCLibraryStep { |
| 1411 | 1381 | .builder = builder, |
| 1412 | 1382 | .step = Step.init(builder.fmt("install {}", lib.step.name), builder.allocator, make), |