@@ -85,14 +85,23 @@ pub fn build(b: *std.Build) void {
8585 exe .root_module .addOptions ("build_options" , mlir_options );
8686 lib_mod .addOptions ("build_options" , mlir_options );
8787
88- // Add include path
88+ // Add include paths
8989 exe .addIncludePath (b .path ("src" ));
9090 lib .addIncludePath (b .path ("src" ));
9191
92+ // Add Ora dialect include path (for OraCAPI.h)
93+ const ora_dialect_include_path = b .path ("src/mlir/ora/include" );
94+ exe .addIncludePath (ora_dialect_include_path );
95+
96+ // Add EthIR dialect include path
97+ const ethir_dialect_include_path = b .path ("src/mlir/IR/include" );
98+ exe .addIncludePath (ethir_dialect_include_path );
99+
92100 // Build and link MLIR (required) - only for executable, not library
93101 const mlir_step = buildMlirLibraries (b , target , optimize );
94102 const ora_dialect_step = buildOraDialectLibrary (b , mlir_step , target , optimize );
95- linkMlirLibraries (b , exe , mlir_step , ora_dialect_step , target );
103+ const ethir_dialect_step = buildEthIRDialectLibrary (b , mlir_step , target , optimize );
104+ linkMlirLibraries (b , exe , mlir_step , ora_dialect_step , ethir_dialect_step , target );
96105
97106 // Build and link Z3 (for formal verification) - only for executable
98107 const z3_step = buildZ3Libraries (b , target , optimize );
@@ -467,8 +476,10 @@ fn buildOraDialectLibraryImpl(step: *std.Build.Step, options: std.Build.Step.Mak
467476 const allocator = b .allocator ;
468477 const cwd = std .fs .cwd ();
469478
470- // Create build directory
479+ // Create build directory (clean if it exists to avoid CMake cache conflicts)
471480 const build_dir = "vendor/ora-dialect-build" ;
481+ // Remove existing build directory to avoid CMake cache conflicts
482+ cwd .deleteTree (build_dir ) catch {};
472483 cwd .makeDir (build_dir ) catch | err | switch (err ) {
473484 error .PathAlreadyExists = > {},
474485 else = > return err ,
@@ -503,7 +514,7 @@ fn buildOraDialectLibraryImpl(step: *std.Build.Step, options: std.Build.Step.Mak
503514 }
504515 try cmake_args .appendSlice (&[_ ][]const u8 {
505516 "-S" ,
506- "src/mlir/generated " ,
517+ "src/mlir/ora " ,
507518 "-B" ,
508519 build_dir ,
509520 "-DCMAKE_BUILD_TYPE=Release" ,
@@ -564,22 +575,149 @@ fn buildOraDialectLibraryImpl(step: *std.Build.Step, options: std.Build.Step.Mak
564575 std .log .info ("Successfully built Ora dialect library" , .{});
565576}
566577
578+ /// Build EthIR dialect library using CMake
579+ fn buildEthIRDialectLibrary (b : * std.Build , mlir_step : * std.Build.Step , target : std.Build.ResolvedTarget , optimize : std.builtin.OptimizeMode ) * std.Build.Step {
580+ _ = target ;
581+ _ = optimize ;
582+
583+ const step = b .allocator .create (std .Build .Step ) catch @panic ("OOM" );
584+ step .* = std .Build .Step .init (.{
585+ .id = .custom ,
586+ .name = "cmake-build-ethir-dialect" ,
587+ .owner = b ,
588+ .makeFn = buildEthIRDialectLibraryImpl ,
589+ });
590+ step .dependOn (mlir_step );
591+ return step ;
592+ }
593+
594+ /// Implementation of CMake build for EthIR dialect library
595+ fn buildEthIRDialectLibraryImpl (step : * std.Build.Step , options : std.Build.Step.MakeOptions ) anyerror ! void {
596+ _ = options ;
597+
598+ const b = step .owner ;
599+ const allocator = b .allocator ;
600+ const cwd = std .fs .cwd ();
601+
602+ // Create build directory (clean if it exists to avoid CMake cache conflicts)
603+ const build_dir = "vendor/ethir-dialect-build" ;
604+ // Remove existing build directory to avoid CMake cache conflicts
605+ cwd .deleteTree (build_dir ) catch {};
606+ cwd .makeDir (build_dir ) catch | err | switch (err ) {
607+ error .PathAlreadyExists = > {},
608+ else = > return err ,
609+ };
610+
611+ const install_prefix = "vendor/mlir" ;
612+ const mlir_dir = b .fmt ("{s}/lib/cmake/mlir" , .{install_prefix });
613+
614+ // Platform-specific flags
615+ const builtin = @import ("builtin" );
616+ var cmake_args = std .array_list .Managed ([]const u8 ).init (allocator );
617+ defer cmake_args .deinit ();
618+
619+ // Prefer Ninja generator when available
620+ var use_ninja : bool = false ;
621+ {
622+ const probe = std .process .Child .run (.{ .allocator = allocator , .argv = &[_ ][]const u8 { "ninja" , "--version" }, .cwd = "." }) catch null ;
623+ if (probe ) | res | {
624+ switch (res .term ) {
625+ .Exited = > | code | {
626+ if (code == 0 ) use_ninja = true ;
627+ },
628+ else = > {},
629+ }
630+ }
631+ }
632+
633+ try cmake_args .append ("cmake" );
634+ if (use_ninja ) {
635+ try cmake_args .append ("-G" );
636+ try cmake_args .append ("Ninja" );
637+ }
638+ try cmake_args .appendSlice (&[_ ][]const u8 {
639+ "-S" ,
640+ "src/mlir/IR" ,
641+ "-B" ,
642+ build_dir ,
643+ "-DCMAKE_BUILD_TYPE=Release" ,
644+ b .fmt ("-DMLIR_DIR={s}" , .{mlir_dir }),
645+ b .fmt ("-DCMAKE_INSTALL_PREFIX={s}" , .{install_prefix }),
646+ });
647+
648+ if (builtin .os .tag == .linux ) {
649+ try cmake_args .append ("-DCMAKE_CXX_FLAGS=-stdlib=libc++ -lc++abi" );
650+ try cmake_args .append ("-DCMAKE_CXX_COMPILER=clang++" );
651+ try cmake_args .append ("-DCMAKE_C_COMPILER=clang" );
652+ } else if (builtin .os .tag == .macos ) {
653+ try cmake_args .append ("-DCMAKE_CXX_FLAGS=-stdlib=libc++" );
654+ }
655+
656+ var cfg_child = std .process .Child .init (cmake_args .items , allocator );
657+ cfg_child .cwd = "." ;
658+ cfg_child .stdin_behavior = .Inherit ;
659+ cfg_child .stdout_behavior = .Inherit ;
660+ cfg_child .stderr_behavior = .Inherit ;
661+ const cfg_term = cfg_child .spawnAndWait () catch | err | {
662+ std .log .err ("Failed to configure EthIR dialect CMake: {}" , .{err });
663+ return err ;
664+ };
665+ switch (cfg_term ) {
666+ .Exited = > | code | if (code != 0 ) {
667+ std .log .err ("EthIR dialect CMake configure failed with exit code: {}" , .{code });
668+ return error .CMakeConfigureFailed ;
669+ },
670+ else = > {
671+ std .log .err ("EthIR dialect CMake configure did not exit cleanly" , .{});
672+ return error .CMakeConfigureFailed ;
673+ },
674+ }
675+
676+ // Build and install
677+ var build_args = [_ ][]const u8 { "cmake" , "--build" , build_dir , "--parallel" , "--target" , "install" };
678+ var build_child = std .process .Child .init (& build_args , allocator );
679+ build_child .cwd = "." ;
680+ build_child .stdin_behavior = .Inherit ;
681+ build_child .stdout_behavior = .Inherit ;
682+ build_child .stderr_behavior = .Inherit ;
683+ const build_term = build_child .spawnAndWait () catch | err | {
684+ std .log .err ("Failed to build EthIR dialect with CMake: {}" , .{err });
685+ return err ;
686+ };
687+ switch (build_term ) {
688+ .Exited = > | code | if (code != 0 ) {
689+ std .log .err ("EthIR dialect CMake build failed with exit code: {}" , .{code });
690+ return error .CMakeBuildFailed ;
691+ },
692+ else = > {
693+ std .log .err ("EthIR dialect CMake build did not exit cleanly" , .{});
694+ return error .CMakeBuildFailed ;
695+ },
696+ }
697+
698+ std .log .info ("Successfully built EthIR dialect library" , .{});
699+ }
700+
567701/// Link MLIR to the given executable using the installed prefix
568- fn linkMlirLibraries (b : * std.Build , exe : * std.Build.Step.Compile , mlir_step : * std.Build.Step , ora_dialect_step : * std.Build.Step , target : std.Build.ResolvedTarget ) void {
702+ fn linkMlirLibraries (b : * std.Build , exe : * std.Build.Step.Compile , mlir_step : * std.Build.Step , ora_dialect_step : * std.Build.Step , ethir_dialect_step : * std.Build.Step , target : std.Build.ResolvedTarget ) void {
569703 // Depend on MLIR build and dialect builds
570704 exe .step .dependOn (mlir_step );
571705 exe .step .dependOn (ora_dialect_step );
706+ exe .step .dependOn (ethir_dialect_step );
572707
573708 const include_path = b .path ("vendor/mlir/include" );
574709 const lib_path = b .path ("vendor/mlir/lib" );
575- const ora_dialect_include_path = b .path ("src/mlir/generated" );
710+ const ora_dialect_include_path = b .path ("src/mlir/ora/include" );
711+ const ethir_dialect_include_path = b .path ("src/mlir/IR/include" );
576712
577713 exe .addIncludePath (include_path );
578714 exe .addIncludePath (ora_dialect_include_path );
715+ exe .addIncludePath (ethir_dialect_include_path );
579716 exe .addLibraryPath (lib_path );
580717
581718 exe .linkSystemLibrary ("MLIR-C" );
582719 exe .linkSystemLibrary ("MLIROraDialectC" );
720+ exe .linkSystemLibrary ("MLIREthIRDialect" );
583721
584722 switch (target .result .os .tag ) {
585723 .linux = > {
0 commit comments