8
8
9
9
10
10
PACKAGE_DIR = Path (__file__ ).parent / "openjpeg"
11
+ BUILD_TOOLS = PACKAGE_DIR .parent / "build_tools"
11
12
OPENJPEG_SRC = PACKAGE_DIR / "src" / "openjpeg" / "src" / "lib" / "openjp2"
12
13
INTERFACE_SRC = PACKAGE_DIR / "src" / "interface"
13
14
@@ -50,6 +51,8 @@ def build(setup_kwargs: Any) -> Any:
50
51
relative_ext = output .relative_to (cmd .build_lib )
51
52
shutil .copyfile (output , relative_ext )
52
53
54
+ reset_oj ()
55
+
53
56
return setup_kwargs
54
57
55
58
@@ -77,14 +80,42 @@ def get_source_files() -> List[Path]:
77
80
78
81
def setup_oj () -> None :
79
82
"""Run custom cmake."""
80
- base_dir = os .path .join ("openjpeg" , "src" , "openjpeg" )
83
+ base_dir = PACKAGE_DIR / "src" / "openjpeg"
84
+ p_openjpeg = base_dir / "src" / "lib" / "openjp2" / "openjpeg.c"
85
+
86
+ # Backup original CMakeLists.txt and openjpeg.c files
87
+ backup_dir = BUILD_TOOLS / "backup"
88
+ if os .path .exists (backup_dir ):
89
+ shutil .rmtree (backup_dir )
90
+
91
+ backup_dir .mkdir (exist_ok = True , parents = True )
92
+
93
+ shutil .copy (
94
+ base_dir / "CMakeLists.txt" ,
95
+ backup_dir / "CMakeLists.txt.backup" ,
96
+ )
97
+ shutil .copy (
98
+ p_openjpeg ,
99
+ backup_dir / "openjpeg.c.backup" ,
100
+ )
81
101
82
102
# Copy custom CMakeLists.txt file to openjpeg base dir
83
103
shutil .copy (
84
- os . path . join ( "build_tools" , "cmake" , "CMakeLists.txt" ) ,
85
- base_dir
104
+ BUILD_TOOLS / "cmake" / "CMakeLists.txt" ,
105
+ base_dir ,
86
106
)
87
- build_dir = os .path .join (base_dir , "build" )
107
+ # Edit openjpeg.c to remove the OPJ_API declaration
108
+ with p_openjpeg .open ("r" ) as f :
109
+ data = f .readlines ()
110
+
111
+ data = [
112
+ line .replace ("OPJ_API " , "" )
113
+ if line .startswith ("OPJ_API " ) else line for line in data
114
+ ]
115
+ with p_openjpeg .open ("w" ) as f :
116
+ f .write ("" .join (data ))
117
+
118
+ build_dir = base_dir / "build"
88
119
if os .path .exists (build_dir ):
89
120
shutil .rmtree (build_dir )
90
121
@@ -106,3 +137,31 @@ def setup_oj() -> None:
106
137
with open (INTERFACE_SRC / "opj_config.h" , "a" ) as f :
107
138
f .write ("\n " )
108
139
f .write ("#define USE_JPIP 0" )
140
+
141
+
142
+ def reset_oj () -> None :
143
+ # Restore submodule to original state
144
+ # Restore CMakeLists.txt and openjpeg.c files
145
+ base_dir = PACKAGE_DIR / "src" / "openjpeg"
146
+ build_dir = base_dir / "build"
147
+ backup_dir = BUILD_TOOLS / "backup"
148
+ p_openjpeg = base_dir / "src" / "lib" / "openjp2" / "openjpeg.c"
149
+
150
+ if (backup_dir / "CMakeLists.txt.backup" ).exists ():
151
+ shutil .copy (
152
+ backup_dir / "CMakeLists.txt.backup" ,
153
+ base_dir / "CMakeLists.txt" ,
154
+ )
155
+
156
+ if (backup_dir / "openjpeg.c.backup" ).exists ():
157
+ shutil .copy (
158
+ BUILD_TOOLS / "backup" / "openjpeg.c.backup" ,
159
+ p_openjpeg ,
160
+ )
161
+
162
+ # Cleanup added directories
163
+ if os .path .exists (build_dir ):
164
+ shutil .rmtree (build_dir )
165
+
166
+ if os .path .exists (backup_dir ):
167
+ shutil .rmtree (backup_dir )
0 commit comments