Skip to content

Commit 234f95f

Browse files
committed
Adds method to access openocd in Windows
1 parent b8e1b66 commit 234f95f

File tree

2 files changed

+62
-14
lines changed

2 files changed

+62
-14
lines changed

tools/openocd/defs.bzl

Lines changed: 58 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ Example:
8282
)
8383

8484
def _openocd_debug_server_impl(ctx):
85+
# is_windows = "windows" in ctx.var['TARGET_CPU']
86+
is_windows = ctx.host_configuration.host_path_separator == ";"
8587
interface_config_string = ""
8688
for config in ctx.attr.interface_configs:
8789
interface_config_string = interface_config_string + " -f " + config
@@ -92,20 +94,66 @@ def _openocd_debug_server_impl(ctx):
9294
if ctx.attr.transport:
9395
transport_string = "-c \"transport select %s\"" % (ctx.attr.transport)
9496
script_template = """
95-
{openocd} {interface_config_string} {transport_string} -c "adapter_khz {programmer_frequency}" {chip_config_string}
97+
{openocd} {interface_config_string} {transport_string} -c "adapter speed {programmer_frequency}" {chip_config_string}
9698
9799
"""
98100

99-
script = ctx.actions.declare_file("%s.sh" % ctx.label.name)
100-
script_content = script_template.format(
101-
openocd = ctx.file._openocd.short_path,
102-
interface_config_string = interface_config_string,
103-
chip_config_string = chip_config_string,
104-
programmer_frequency = ctx.attr.programmer_frequency,
105-
transport_string = transport_string,
106-
)
101+
script_content = ""
102+
if is_windows:
103+
# A bit of a hacky way to do this... The issue is Windows does not support
104+
# symlinks which does not work well for runfiles in particular. To enable them
105+
# the user must use Developer Mode, also not ideal. Instead:
106+
# 1) Use the 'symlink' as a generic path to the root of openocd
107+
# 2) Open the MANIFEST file (non-standard format, beware for future versions)
108+
# 3) Find com_openocd and use the full path to the symlink
109+
# 4) In batch script, cd to base of tool and call bin/openocd.exe directly
110+
windows_script_prepend = """
111+
@echo off
112+
for /f "tokens=*" %%a in (MANIFEST) do call :SEARCH %%a %%a
113+
114+
:SEARCH
115+
set str1=%1
116+
if /i "%str1:~0,11%"=="com_openocd" goto FOUND
117+
118+
goto FAIL
119+
120+
:FOUND
121+
for %%i in (%2) do call :MAKEPATH %%~di%%~pi
122+
123+
goto EOF
124+
125+
:MAKEPATH
126+
cd %1
127+
128+
129+
"""
130+
windows_script_append = """
131+
goto EOF
132+
133+
:FAIL
134+
135+
:EOF
136+
137+
"""
138+
script = ctx.actions.declare_file("%s.bat" % ctx.label.name)
139+
script_content = windows_script_prepend + script_template.format(
140+
openocd = "\"bin/openocd.exe\"",
141+
interface_config_string = interface_config_string,
142+
chip_config_string = chip_config_string,
143+
programmer_frequency = ctx.attr.programmer_frequency,
144+
transport_string = transport_string,
145+
) + windows_script_append
146+
else:
147+
script = ctx.actions.declare_file("%s.sh" % ctx.label.name)
148+
script_content = script_template.format(
149+
openocd = ctx.file._openocd.short_path,
150+
interface_config_string = interface_config_string,
151+
chip_config_string = chip_config_string,
152+
programmer_frequency = ctx.attr.programmer_frequency,
153+
transport_string = transport_string,
154+
)
107155
ctx.actions.write(script, script_content, is_executable = True)
108-
runfiles = ctx.runfiles(files = [ctx.file._openocd])
156+
runfiles = ctx.runfiles(files = [ctx.file._openocd] )
109157
return [DefaultInfo(executable = script, runfiles = runfiles)]
110158

111159
openocd_debug_server = rule(

tools/openocd/openocd_repository.bzl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
def _get_platform_specific_config(os_name):
22
_WINDOWS = {
3-
"sha256": "1fb26bbcfd65dbabe747ce3c8467a1f1cece7253bde4a95de13c2267d422ed8b",
4-
"prefix": "xpack-openocd-0.10.0-14",
5-
"url": "https://github.com/xpack-dev-tools/openocd-xpack/releases/download/v0.10.0-14/xpack-openocd-0.10.0-14-win32-x64.zip",
3+
"sha256": "b195e3c3e9fe3f4fe9dfd9780ff08336ef8ed3015a76cc883d9e21e57c456f33",
4+
"prefix": "",
5+
"url": "https://github.com/ntfreak/openocd/releases/download/v0.11.0/openocd-v0.11.0-i686-w64-mingw32.tar.gz",
66
}
77
_PLATFORM_SPECIFIC_CONFIGS = {
88
"mac os x": {
@@ -45,8 +45,8 @@ def _openocd_repository_impl(repository_ctx):
4545
executable_extension = ""
4646
if "windows" in repository_ctx.os.name:
4747
executable_extension = ".exe"
48-
repository_ctx.symlink("bin/openocd"+ executable_extension, "openocd" )
4948

49+
repository_ctx.symlink("bin/openocd"+ executable_extension, "openocd" )
5050
repository_ctx.file(
5151
"BUILD",
5252
content = """

0 commit comments

Comments
 (0)