@@ -20,6 +20,11 @@ pub fn build(b: *std.Build) !void {
2020
2121 const blst_dep = b .dependency ("blst" , .{ .target = target , .optimize = optimize });
2222
23+ // passed by "zig build -Dportable=true"
24+ const portable = b .option (bool , "portable" , "Enable portable implementation" ) orelse false ;
25+ // passed by "zig build -Dforce-adx=true"
26+ const force_adx = b .option (bool , "force-adx" , "Enable ADX optimizations" ) orelse false ;
27+
2328 // build blst-z static library
2429 const staticLib = b .addStaticLibrary (.{
2530 .name = "blst-z" ,
@@ -30,18 +35,28 @@ pub fn build(b: *std.Build) !void {
3035 .optimize = optimize ,
3136 });
3237
33- // passed by "zig build -Dportable=true"
34- const portable = b .option (bool , "portable" , "Enable portable implementation" ) orelse false ;
35- // passed by "zig build -Dforce-adx=true"
36- const force_adx = b .option (bool , "force-adx" , "Enable ADX optimizations" ) orelse false ;
38+ const module_blst_min_pk = b .createModule (.{
39+ .root_source_file = b .path ("src/root_min_pk.zig" ),
40+ .target = target ,
41+ .optimize = optimize ,
42+ });
43+ try withBlst (blst_dep , module_blst_min_pk , target , false , portable , force_adx );
44+
45+ b .modules .put (b .dupe ("blst_min_pk" ), module_blst_min_pk ) catch @panic ("OOM" );
46+
47+ const module_blst_min_sig = b .createModule (.{
48+ .root_source_file = b .path ("src/root_min_sig.zig" ),
49+ .target = target ,
50+ .optimize = optimize ,
51+ });
52+ try withBlst (blst_dep , module_blst_min_sig , target , false , portable , force_adx );
53+ b .modules .put (b .dupe ("blst_min_sig" ), module_blst_min_sig ) catch @panic ("OOM" );
3754
3855 // blst does not need libc, however we need to link it to enable threading
3956 // see https://github.com/ChainSafe/blst-bun/issues/4
4057 staticLib .linkLibC ();
41- try withBlst (blst_dep , staticLib , target , false , portable , force_adx );
42-
4358 // the folder where blst.h is located
44- staticLib . addIncludePath (blst_dep . path ( "bindings" ) );
59+ try withBlst (blst_dep , staticLib . root_module , target , false , portable , force_adx );
4560
4661 // This declares intent for the library to be installed into the standard
4762 // location when the user invokes the "install" step (the default step when
@@ -59,8 +74,7 @@ pub fn build(b: *std.Build) !void {
5974 // blst does not need libc, however we need to link it to enable threading
6075 // see https://github.com/ChainSafe/blst-bun/issues/4
6176 sharedLib .linkLibC ();
62- try withBlst (blst_dep , sharedLib , target , true , portable , force_adx );
63- sharedLib .addIncludePath (blst_dep .path ("bindings" ));
77+ try withBlst (blst_dep , sharedLib .root_module , target , true , portable , force_adx );
6478 b .installArtifact (sharedLib );
6579
6680 const exe = b .addExecutable (.{
@@ -106,8 +120,7 @@ pub fn build(b: *std.Build) !void {
106120 .optimize = optimize ,
107121 });
108122
109- lib_unit_tests .linkLibrary (staticLib );
110- lib_unit_tests .addIncludePath (blst_dep .path ("bindings" ));
123+ try withBlst (blst_dep , lib_unit_tests .root_module , target , true , portable , force_adx );
111124
112125 const run_lib_unit_tests = b .addRunArtifact (lib_unit_tests );
113126
@@ -131,7 +144,8 @@ pub fn build(b: *std.Build) !void {
131144/// and zig will handle a mixture of C, assembly and Zig code
132145/// reference to https://github.com/supranational/blst/blob/v0.3.13/bindings/rust/build.rs
133146/// TODO: port all missing flows from the Rust build script
134- fn withBlst (blst_dep : * std.Build.Dependency , blst_z_lib : * Compile , target : ResolvedTarget , is_shared_lib : bool , portable : bool , force_adx : bool ) ! void {
147+ fn withBlst (blst_dep : * std.Build.Dependency , module : * std.Build.Module , target : ResolvedTarget , is_shared_lib : bool , portable : bool , force_adx : bool ) ! void {
148+ module .addIncludePath (blst_dep .path ("bindings" ));
135149 // add later, once we have cflags
136150 const arch = target .result .cpu .arch ;
137151
@@ -141,10 +155,10 @@ fn withBlst(blst_dep: *std.Build.Dependency, blst_z_lib: *Compile, target: Resol
141155 if (portable == true and force_adx == false ) {
142156 // TODO: panic if target_env is sgx
143157 // use this instead
144- blst_z_lib . root_module .addCMacro ("__BLST_PORTABLE__" , "" );
158+ module .addCMacro ("__BLST_PORTABLE__" , "" );
145159 } else if (portable == false and force_adx == true ) {
146160 if (arch == .x86_64 ) {
147- blst_z_lib . root_module .addCMacro ("__ADX__" , "" );
161+ module .addCMacro ("__ADX__" , "" );
148162 } else {
149163 std .debug .print ("`force-adx` is ignored for non-x86_64 targets \n " , .{});
150164 }
@@ -153,16 +167,14 @@ fn withBlst(blst_dep: *std.Build.Dependency, blst_z_lib: *Compile, target: Resol
153167 // if std::is_x86_feature_detected!("adx") {
154168 if (arch == .x86_64 ) {
155169 std .debug .print ("ADX is turned on by default for x86_64 targets \n " , .{});
156- blst_z_lib . root_module .addCMacro ("__ADX__" , "" );
170+ module .addCMacro ("__ADX__" , "" );
157171 }
158172 // otherwise get: "undefined symbol redcx_mont_256" when run tests in Linux
159173 } else {
160174 // both are true
161175 @panic ("Cannot set both `portable` and `force-adx` to true" );
162176 }
163177
164- blst_z_lib .no_builtin = true ;
165-
166178 var gpa = std .heap .GeneralPurposeAllocator (.{}){};
167179 const allocator = gpa .allocator ();
168180
@@ -184,8 +196,8 @@ fn withBlst(blst_dep: *std.Build.Dependency, blst_z_lib: *Compile, target: Resol
184196 try cflags .append ("-fPIC" );
185197 }
186198
187- blst_z_lib .addCSourceFile (.{ .file = blst_dep .path ("src/server.c" ), .flags = cflags .items });
188- blst_z_lib .addCSourceFile (.{ .file = blst_dep .path ("build/assembly.S" ), .flags = cflags .items });
199+ module .addCSourceFile (.{ .file = blst_dep .path ("src/server.c" ), .flags = cflags .items });
200+ module .addCSourceFile (.{ .file = blst_dep .path ("build/assembly.S" ), .flags = cflags .items });
189201
190202 // TODO: we may not need this since we linkLibC() above
191203 const os = target .result .os ;
@@ -195,12 +207,12 @@ fn withBlst(blst_dep: *std.Build.Dependency, blst_z_lib: *Compile, target: Resol
195207 if (os .tag == .linux ) {
196208 // since "zig cc" works fine, we just follow it
197209 // zig cc -E -Wp,-v -
198- blst_z_lib .addIncludePath (.{ .cwd_relative = "/usr/local/include" });
199- blst_z_lib .addIncludePath (.{ .cwd_relative = "/usr/include" });
210+ module .addIncludePath (.{ .cwd_relative = "/usr/local/include" });
211+ module .addIncludePath (.{ .cwd_relative = "/usr/include" });
200212 if (arch == .x86_64 ) {
201- blst_z_lib .addIncludePath (.{ .cwd_relative = "/usr/include/x86_64-linux-gnu" });
213+ module .addIncludePath (.{ .cwd_relative = "/usr/include/x86_64-linux-gnu" });
202214 } else if (arch == .aarch64 ) {
203- blst_z_lib .addIncludePath (.{ .cwd_relative = "/usr/include/aarch64-linux-gnu" });
215+ module .addIncludePath (.{ .cwd_relative = "/usr/include/aarch64-linux-gnu" });
204216 }
205217 }
206218}
0 commit comments