Skip to content

Commit 95da581

Browse files
committed
Fix Windows MuJoCo initialization and NASM linking
1 parent 185a606 commit 95da581

2 files changed

Lines changed: 48 additions & 21 deletions

File tree

third_party/mujoco/windows_msvc_compat.patch

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,21 @@ index ddb1db9..ddb5ef0 100644
2727

2828
mju_closeResource(resource);
2929
return spec;
30+
diff --git a/include/mujoco/mjplugin.h b/include/mujoco/mjplugin.h
31+
--- a/include/mujoco/mjplugin.h
32+
+++ b/include/mujoco/mjplugin.h
33+
@@ -203 +203 @@
34+
- #pragma section(".CRT$XCU", read)
35+
+ #pragma section(".CRT$XCV", read)
36+
@@ -213,9 +213,9 @@
37+
#define mjPLUGIN_LIB_INIT(n) \
38+
static void __cdecl _mj_init_##n(void); \
39+
/* use mjEXTERNC to prevent C++ name mangling */ \
40+
- /* allocate the function pointer to the .CRT$XCU section of the executable */ \
41+
+ /* allocate the function pointer to the .CRT$XCV section of the executable */ \
42+
/* functions in this section are executed on startup before calling main() */ \
43+
- mjEXTERNC __declspec(allocate(".CRT$XCU")) \
44+
+ mjEXTERNC __declspec(allocate(".CRT$XCV")) \
45+
void (__cdecl * _mj_ptr_##n)(void) = _mj_init_##n; \
46+
/* Force the linker to include the pointer symbol */ \
47+
__pragma(comment(linker, "/include:" LINKER_NAME #n)) \

third_party/nasm/nasm.BUILD

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
# Description:
1616
# NASM is a portable assembler in the Intel/Microsoft tradition.
1717

18-
load("@rules_cc//cc:defs.bzl", "cc_binary")
18+
load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_library")
1919

2020
licenses(["notice"]) # BSD 2-clause
2121

@@ -31,26 +31,29 @@ genrule(
3131
cmd = "cp $(location @//third_party/nasm:config.h) $@",
3232
)
3333

34-
cc_binary(
35-
name = "nasm",
36-
srcs = glob([
37-
"asm/*.c",
38-
"asm/*.h",
39-
"autoconf/*.h",
40-
"common/*.c",
41-
"config/*.h",
42-
"include/*.h",
43-
"macros/*.c",
44-
"nasmlib/*.c",
45-
"nasmlib/*.h",
46-
"output/*.c",
47-
"output/*.h",
48-
"stdlib/*.c",
49-
"x86/*.c",
50-
"x86/*.h",
51-
"zlib/*.c",
52-
"zlib/*.h",
53-
]) + [
34+
cc_library(
35+
name = "nasm_lib",
36+
srcs = glob(
37+
[
38+
"asm/*.c",
39+
"asm/*.h",
40+
"autoconf/*.h",
41+
"common/*.c",
42+
"config/*.h",
43+
"include/*.h",
44+
"macros/*.c",
45+
"nasmlib/*.c",
46+
"nasmlib/*.h",
47+
"output/*.c",
48+
"output/*.h",
49+
"stdlib/*.c",
50+
"x86/*.c",
51+
"x86/*.h",
52+
"zlib/*.c",
53+
"zlib/*.h",
54+
],
55+
exclude = ["asm/nasm.c"],
56+
) + [
5457
"version.h",
5558
":config_h",
5659
],
@@ -79,7 +82,13 @@ cc_binary(
7982
"x86",
8083
"zlib",
8184
],
85+
)
86+
87+
cc_binary(
88+
name = "nasm",
89+
srcs = ["asm/nasm.c"],
8290
visibility = ["@libjpeg_turbo//:__pkg__"],
91+
deps = [":nasm_lib"],
8392
)
8493

8594
config_setting(

0 commit comments

Comments
 (0)