@@ -5,98 +5,72 @@ package(default_visibility = ["//visibility:public"])
55exports_files(glob["**/*"])
66"""
77
8- _ECSACT_TOOLCHAINS_BUILD_FILE_CONTENTS = """
9- load("@rules_ecsact//ecsact:toolchain.bzl", "ecsact_toolchain")
10-
11- package(default_visibility = ["//visibility:public"])
12-
13- ecsact_toolchain(
14- name = "ecsact_sdk_system",
15- target_tool_path = "{ecsact_exe_path}",
16- )
17-
18- toolchain(
19- name = "ecsact_sdk_system_toolchain",
20- toolchain = ":ecsact_sdk_system",
21- toolchain_type = "@rules_ecsact//ecsact:toolchain_type",
22- )
8+ _ECSACT_TOOLCHAIN_SDK = """
9+ # Ecsact toolchain from your locally installed SDK
10+ # https://github.com/ecsact-dev/ecsact_sdk
11+ ecsact_toolchain(name = "ecsact_sdk_system", target_tool_path = "{ecsact_exe_path}")
12+ toolchain(name = "ecsact_sdk_system_toolchain", toolchain = ":ecsact_sdk_system", toolchain_type = "@rules_ecsact//ecsact:toolchain_type")
2313"""
2414
25- _ECSACT_SDK_BUILD_FILE_CONTENTS = """
26- load("@rules_ecsact//ecsact:defs.bzl", "ecsact_codegen_plugin")
27-
28- package(default_visibility = ["//visibility:public"])
29-
30- ecsact_codegen_plugin(
31- name = "cpp_header",
32- output_extension = "hh",
33- plugin_path = "cpp_header",
34- )
15+ _ECSACT_TOOLCHAIN_CLI = """
16+ # Ecsact toolchain from the ecsact_cli bazel module
17+ # https://github.com/ecsact-dev/ecsact_cli
18+ ecsact_toolchain(name = "ecsact_cli", target_tool = "@ecsact_cli")
19+ toolchain(name = "ecsact_cli_toolchain", toolchain = ":ecsact_cli", toolchain_type = "@rules_ecsact//ecsact:toolchain_type")
3520"""
3621
37- def _ecsact_sdk_repository_impl (rctx ):
38- rctx .file (
39- "BUILD.bazel" ,
40- executable = False ,
41- content = "" ,
42- )
22+ def _ecsact_toolchain_repository_impl (rctx ):
23+ # type: (repository_ctx) -> None
4324
44- rctx .file (
45- "codegen_plugins/BUILD.bazel" ,
46- executable = False ,
47- content = _ECSACT_SDK_BUILD_FILE_CONTENTS ,
48- )
25+ build_file_contents = 'load("@rules_ecsact//ecsact:toolchain.bzl", "ecsact_toolchain")\n '
26+ build_file_contents += 'package(default_visibility = ["//visibility:public"])\n \n '
4927
50- _ecsact_sdk_repository = repository_rule (
51- implementation = _ecsact_sdk_repository_impl ,
52- attrs = {
53- },
54- )
28+ if rctx .attr .ecsact_system_sdk_exe :
29+ build_file_contents += _ECSACT_TOOLCHAIN_SDK .format (
30+ ecsact_exe_path = rctx .attr .ecsact_system_sdk_exe .replace ("\\ " , "/" ),
31+ )
32+
33+ if rctx .attr .use_ecsact_cli :
34+ build_file_contents += _ECSACT_TOOLCHAIN_CLI
5535
56- def _ecsact_toolchains_repository_impl (rctx ):
5736 rctx .file (
5837 "BUILD.bazel" ,
5938 executable = False ,
60- content = _ECSACT_TOOLCHAINS_BUILD_FILE_CONTENTS .format (
61- ecsact_exe_path = rctx .attr .ecsact_exe .replace ("\\ " , "/" ),
62- ),
39+ content = build_file_contents ,
6340 )
6441
65- _ecsact_toolchains_repository = repository_rule (
66- implementation = _ecsact_toolchains_repository_impl ,
42+ _ecsact_toolchain_repository = repository_rule (
43+ implementation = _ecsact_toolchain_repository_impl ,
6744 attrs = {
68- "ecsact_exe" : attr .string (mandatory = True ),
45+ "ecsact_system_sdk_exe" : attr .string (mandatory = False ),
46+ "use_ecsact_cli" : attr .bool (mandatory = True ),
6947 },
7048)
7149
72- def _windows_find_ecsact_from_app_installer (ctx ):
50+ def _windows_find_ecsact_from_app_installer (mctx ):
51+ # type: (module_ctx) -> path
52+
7353 # ctx.which doesn't work for everything available on Windows. Using cmd's
7454 # built-in 'where' command can find programs installed from the Microsoft
7555 # store or MSIX packages.
76- cmd = ctx .which ("cmd.exe" )
56+ cmd = mctx .which ("cmd.exe" )
7757 if cmd :
78- where_result = ctx .execute ([cmd , "/C" , "where ecsact.exe" ])
58+ where_result = mctx .execute ([cmd , "/C" , "where ecsact.exe" ])
7959 if where_result .stdout :
80- return ctx .path (where_result .stdout .strip ())
60+ return mctx .path (where_result .stdout .strip ())
8161
8262 return None
8363
84- def _ecsact_impl (mctx ):
85- wanted_ecsact_version = None
86-
87- for mod in mctx .modules :
88- for sdk_toolchain in mod .tags .sdk_toolchain :
89- if wanted_ecsact_version != None :
90- fail ("ecsact extension sdk_toolchain called multiple times. Please only call it once." )
91- wanted_ecsact_version = sdk_toolchain .version
64+ def _get_escact_system_sdk (mctx , wanted_ecsact_version = None ):
65+ # type: (module_ctx, string) -> string
9266
9367 ecsact_exe = mctx .which ("ecsact" )
9468
9569 if ecsact_exe == None and mctx .os .name .startswith ("windows" ):
9670 ecsact_exe = _windows_find_ecsact_from_app_installer (mctx )
9771
9872 if ecsact_exe == None :
99- fail ( "Cannot find the Ecsact SDK installed on your system. See https://ecsact.dev/start for instructions." )
73+ return None
10074
10175 ecsact_version_output = mctx .execute ([ecsact_exe , "--version" ])
10276 if ecsact_version_output .return_code != 0 :
@@ -113,19 +87,41 @@ def _ecsact_impl(mctx):
11387 if found_ecsact_version != wanted_ecsact_version and found_ecsact_version != "refs/tags/{}" .format (wanted_ecsact_version ):
11488 fail ("Wanted Ecsact SDK {}, but {} is installed on your system" .format (wanted_ecsact_version , found_ecsact_version ))
11589
116- _ecsact_sdk_repository (
117- name = "ecsact_sdk" ,
118- )
119- _ecsact_toolchains_repository (
120- name = "ecsact_toolchains" ,
121- ecsact_exe = str (ecsact_exe ),
90+ return ecsact_exe
91+
92+ def _ecsact_impl (mctx ):
93+ # type: (module_ctx) -> None
94+
95+ wanted_ecsact_version = None
96+ use_ecsact_cli = False
97+
98+ for mod in mctx .modules :
99+ for toolchain in mod .tags .toolchain :
100+ if toolchain .use_ecsact_cli :
101+ use_ecsact_cli = True
102+
103+ if toolchain .version :
104+ if wanted_ecsact_version != None :
105+ fail ("ecsact extension toolchain called multiple times. Please only call it once." )
106+ wanted_ecsact_version = toolchain .version
107+
108+ ecsact_exe = _get_escact_system_sdk (mctx , wanted_ecsact_version )
109+
110+ if ecsact_exe != None :
111+ ecsact_exe = str (ecsact_exe )
112+
113+ _ecsact_toolchain_repository (
114+ name = "ecsact_toolchain" ,
115+ ecsact_system_sdk_exe = ecsact_exe ,
116+ use_ecsact_cli = use_ecsact_cli ,
122117 )
123118
124119ecsact = module_extension (
125120 implementation = _ecsact_impl ,
126121 tag_classes = {
127- "sdk_toolchain" : tag_class (attrs = {
128- "version" : attr .string (mandatory = True ),
122+ "toolchain" : tag_class (attrs = {
123+ "use_ecsact_cli" : attr .bool (mandatory = False , default = False ),
124+ "version" : attr .string (mandatory = False ),
129125 }),
130126 },
131127)
0 commit comments