-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathbuild.py
More file actions
executable file
·295 lines (241 loc) · 10 KB
/
build.py
File metadata and controls
executable file
·295 lines (241 loc) · 10 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
#!/usr/bin/env python3
"""
CsharpFlow Build Script for Ubuntu/Linux
Provides easy building with CMake or direct tools
"""
import os
import sys
import shutil
import argparse
import subprocess
from pathlib import Path
from typing import Optional, List
class Colors:
"""ANSI color codes for terminal output"""
RED = '\033[0;31m'
GREEN = '\033[0;32m'
YELLOW = '\033[1;33m'
BLUE = '\033[0;34m'
NC = '\033[0m' # No Color
class BuildScript:
def __init__(self):
self.script_dir = Path(__file__).parent.absolute()
self.bin_dir = self.script_dir / "Bin"
def print_status(self, message: str):
"""Print status message in green"""
print(f"{Colors.GREEN}[INFO]{Colors.NC} {message}")
def print_warning(self, message: str):
"""Print warning message in yellow"""
print(f"{Colors.YELLOW}[WARN]{Colors.NC} {message}")
def print_error(self, message: str):
"""Print error message in red"""
print(f"{Colors.RED}[ERROR]{Colors.NC} {message}")
def check_tool(self, tool_name: str) -> Optional[str]:
"""Check if a tool is available and return its path"""
tool_path = shutil.which(tool_name)
if tool_path:
self.print_status(f"Found {tool_name}: {tool_path}")
return tool_path
return None
def run_command(self, command: List[str], cwd: Optional[Path] = None) -> bool:
"""Run a command and return success status"""
try:
cmd_str = ' '.join(command)
self.print_status(f"Running: {cmd_str}")
result = subprocess.run(
command,
cwd=cwd or self.script_dir,
check=True,
capture_output=False
)
return True
except subprocess.CalledProcessError as e:
self.print_error(f"Command failed with exit code {e.returncode}")
return False
except FileNotFoundError:
self.print_error(f"Command not found: {command[0]}")
return False
def create_directories(self):
"""Create necessary build directories"""
self.print_status("Creating build directories...")
(self.bin_dir / "Debug").mkdir(parents=True, exist_ok=True)
(self.bin_dir / "Release").mkdir(parents=True, exist_ok=True)
def clean_build(self):
"""Clean previous build artifacts"""
self.print_status("Cleaning previous build...")
# Remove build directory
build_dir = self.script_dir / "build"
if build_dir.exists():
shutil.rmtree(build_dir)
# Clean Bin directory
if self.bin_dir.exists():
shutil.rmtree(self.bin_dir)
def build_with_cmake(self, build_type: str, unity_support: bool) -> bool:
"""Build using CMake"""
self.print_status("Building with CMake...")
if not self.check_tool("cmake"):
self.print_error("CMake not found. Please install cmake:")
print(" sudo apt install cmake")
return False
# Prepare CMake arguments
cmake_args = ["-B", "build", f"-DCMAKE_BUILD_TYPE={build_type}"]
if unity_support:
cmake_args.append("-DUNITY_BUILD=ON")
# Configure
self.print_status("Configuring with CMake...")
if not self.run_command(["cmake"] + cmake_args):
return False
# Build
self.print_status("Building with CMake...")
if not self.run_command(["cmake", "--build", "build", "--config", build_type]):
return False
return True
def build_direct(self, build_type: str) -> bool:
"""Build directly with .NET tools"""
self.print_status("Building directly with .NET tools...")
output_dir = str(self.bin_dir / build_type)
# Try different build tools in order of preference
if self.check_tool("dotnet"):
self.print_status("Using .NET CLI...")
# Build Flow library
self.print_status("Building Flow library...")
if not self.run_command(["dotnet", "build", "Flow.csproj", "-c", build_type, "-o", output_dir]):
return False
# Build Flow tests
self.print_status("Building Flow tests...")
if not self.run_command(["dotnet", "build", "TestFlow/TestFlow.csproj", "-c", build_type, "-o", output_dir]):
return False
elif self.check_tool("msbuild"):
self.print_status("Using MSBuild...")
# Build Flow library
self.print_status("Building Flow library...")
if not self.run_command([
"msbuild", "Flow.csproj",
f"/p:Configuration={build_type}",
f"/p:OutputPath={output_dir}/"
]):
return False
# Build Flow tests
self.print_status("Building Flow tests...")
if not self.run_command([
"msbuild", "TestFlow/TestFlow.csproj",
f"/p:Configuration={build_type}",
f"/p:OutputPath={output_dir}/"
]):
return False
elif self.check_tool("xbuild"):
self.print_warning("Using legacy xbuild (consider upgrading to dotnet or msbuild)...")
# Build Flow library
self.print_status("Building Flow library...")
if not self.run_command([
"xbuild", "Flow.csproj",
f"/p:Configuration={build_type}",
f"/p:OutputPath={output_dir}/"
]):
return False
# Build Flow tests
self.print_status("Building Flow tests...")
if not self.run_command([
"xbuild", "TestFlow/TestFlow.csproj",
f"/p:Configuration={build_type}",
f"/p:OutputPath={output_dir}/"
]):
return False
else:
self.print_error("No suitable .NET build tool found!")
print("Please install one of the following:")
print(" - .NET SDK: sudo apt install dotnet-sdk-6.0")
print(" - Mono: sudo apt install mono-devel")
return False
return True
def show_build_results(self, build_type: str):
"""Show information about built files"""
build_dir = self.bin_dir / build_type
flow_dll = build_dir / "Flow.dll"
if flow_dll.exists():
size = flow_dll.stat().st_size
self.print_status(f"Flow.dll: {size:,} bytes")
# Look for test binary
test_files = list(build_dir.glob("TestFlow.*"))
if test_files:
test_file = test_files[0]
size = test_file.stat().st_size
self.print_status(f"Test binary: {test_file.name} ({size:,} bytes)")
def main(self):
"""Main build script entry point"""
parser = argparse.ArgumentParser(
description="CsharpFlow Build Script for Ubuntu/Linux",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="""
Examples:
%(prog)s # Build in Release mode with CMake
%(prog)s --debug # Build in Debug mode
%(prog)s --no-cmake # Build directly with dotnet/mono
%(prog)s --unity --debug # Build with Unity support in Debug mode
"""
)
parser.add_argument(
"-d", "--debug",
action="store_const",
const="Debug",
dest="build_type",
help="Build in Debug mode"
)
parser.add_argument(
"-r", "--release",
action="store_const",
const="Release",
dest="build_type",
help="Build in Release mode (default)"
)
parser.add_argument(
"--no-cmake",
action="store_true",
help="Skip CMake and use direct build tools"
)
parser.add_argument(
"-u", "--unity",
action="store_true",
help="Enable Unity support"
)
parser.add_argument(
"-c", "--clean",
action="store_true",
help="Clean build (remove build directory)"
)
args = parser.parse_args()
# Set defaults
build_type = args.build_type or "Release"
use_cmake = not args.no_cmake
self.print_status("CsharpFlow Build Script")
self.print_status(f"Build Type: {build_type}")
self.print_status(f"Unity Support: {args.unity}")
self.print_status(f"Use CMake: {use_cmake}")
# Clean if requested
if args.clean:
self.clean_build()
# Create directories
self.create_directories()
# Build
success = False
if use_cmake:
success = self.build_with_cmake(build_type, args.unity)
else:
success = self.build_direct(build_type)
if not success:
self.print_error("Build failed!")
return 1
self.print_status("Build completed successfully!")
self.print_status(f"Binaries are in: Bin/{build_type}/")
# Show results
self.show_build_results(build_type)
self.print_status("Build script completed successfully!")
print()
print("Next steps:")
print(" - Run tests: python3 run_tests.py")
print(f" - Or manually: dotnet test TestFlow/TestFlow.csproj")
print(f" - Or with nunit-console: nunit-console Bin/{build_type}/TestFlow.dll")
return 0
if __name__ == "__main__":
builder = BuildScript()
sys.exit(builder.main())