-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathCMakeLists.txt
More file actions
600 lines (514 loc) · 25.3 KB
/
Copy pathCMakeLists.txt
File metadata and controls
600 lines (514 loc) · 25.3 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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
cmake_minimum_required(VERSION 3.18.0)
if (POLICY CMP0141)
cmake_policy(SET CMP0141 NEW)
set(CMAKE_MSVC_DEBUG_INFORMATION_FORMAT "$<IF:$<AND:$<C_COMPILER_ID:MSVC>,$<CXX_COMPILER_ID:MSVC>>,$<$<CONFIG:Debug,RelWithDebInfo>:EditAndContinue>,$<$<CONFIG:Debug,RelWithDebInfo>:ProgramDatabase>>")
endif()
project ("MKF_Project" VERSION 1.0.0 LANGUAGES CXX)
# ============================================================================
# C++ Standard and Compiler Settings
# ============================================================================
set(CMAKE_CXX_STANDARD 23)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
# Let user configure build type; default to Release if not set
if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
message(STATUS "Setting build type to 'Release' as none was specified.")
set(CMAKE_BUILD_TYPE Release CACHE STRING "Choose the type of build." FORCE)
set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug" "Release" "MinSizeRel" "RelWithDebInfo")
endif()
if (CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /bigobj")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /Ox")
# Use /W3 instead of /W0 to catch potential issues; suppress only specific warnings if needed
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W3")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /wd4996") # Suppress deprecation warnings
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /wd4267") # Suppress size_t to int conversion warnings
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} /FS")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /FS")
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
else ()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Wno-deprecated-declarations -Wno-unused-parameter -Wno-switch -Wno-format-truncation")
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
# Set flags for Debug builds (no ASan, no profiling for faster test execution)
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -g")
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -g")
endif()
# ============================================================================
# Build Options
# ============================================================================
option(EMBED_MAS_DATA "Embed data in executable" ON)
option(EMBED_MAS_CORES "Embed cores in executable" ON)
option(EMBED_MAS_CORE_SHAPES "Embed core shapes in executable" ON)
option(EMBED_MAS_CORE_MATERIALS "Embed core materials in executable" ON)
option(EMBED_MAS_WIRES "Embed wires in executable" ON)
option(EMBED_MAS_WIRE_MATERIALS "Embed wire materials in executable" ON)
option(EMBED_MAS_BOBBINS "Embed bobbins in executable" ON)
option(EMBED_MAS_INSULATION_MATERIALS "Embed insulation materials in executable" ON)
option(INCLUDE_MKF_TESTS "Compile the test suite" ON)
option(BUILD_TESTS "Build tests (for dependencies)" OFF)
option(BUILD_EXAMPLES "Build examples (for dependencies)" OFF)
option(BUILD_DEMO "Build demo (for dependencies)" OFF)
# REMOVED: # REMOVED: option(HAVE_LAPACK "Use LAPACK" OFF) — no longer needed, levmar replaced by Eigen — no longer needed, levmar replaced by Eigen
option(ENABLE_NGSPICE "Enable ngspice integration for circuit simulation" OFF)
message(STATUS "Build type: ${CMAKE_BUILD_TYPE}")
message(STATUS "ngspice integration: ${ENABLE_NGSPICE}")
message(STATUS "Embedding MAS Data: ${EMBED_MAS_DATA}")
message(STATUS "Embedding MAS Cores: ${EMBED_MAS_CORES}")
message(STATUS "Embedding MAS CoreShapes: ${EMBED_MAS_CORE_SHAPES}")
message(STATUS "Embedding MAS CoreMaterials: ${EMBED_MAS_CORE_MATERIALS}")
message(STATUS "Embedding MAS Wires: ${EMBED_MAS_WIRES}")
message(STATUS "Embedding MAS WireMaterials: ${EMBED_MAS_WIRE_MATERIALS}")
message(STATUS "Embedding MAS Bobbins: ${EMBED_MAS_BOBBINS}")
message(STATUS "Embedding MAS InsulationMaterials: ${EMBED_MAS_INSULATION_MATERIALS}")
message(STATUS "Including MKF Tests: ${INCLUDE_MKF_TESTS}")
# ============================================================================
# Dependencies via FetchContent
# ============================================================================
include(FetchContent)
set(FETCHCONTENT_QUIET FALSE)
# CMakeRC - Resource compiler (use FetchContent instead of file download)
FetchContent_Declare(cmrc
GIT_REPOSITORY https://github.com/vector-of-bool/cmrc.git
GIT_TAG 952ffddba731fc110bd50409e8d2b8a06abbd237) # was 'master' (floating); pinned to known-good
FetchContent_MakeAvailable(cmrc)
include("${cmrc_SOURCE_DIR}/CMakeRC.cmake")
# JSON library
FetchContent_Declare(json
GIT_REPOSITORY https://github.com/nlohmann/json/
GIT_TAG tags/v3.11.3)
FetchContent_MakeAvailable(json)
include_directories("${CMAKE_BINARY_DIR}/_deps/json-src/include/nlohmann/")
include_directories("${CMAKE_BINARY_DIR}/_deps/json-src/include/")
FetchContent_Declare(magic-enum
GIT_REPOSITORY https://github.com/Neargye/magic_enum
GIT_TAG tags/v0.9.6)
FetchContent_MakeAvailable(magic-enum)
include_directories("${CMAKE_BINARY_DIR}/_deps/magic-enum-src/include/magic_enum")
# Make profiling and ASan opt-in so users can build fast Release binaries by default
option(ENABLE_PROFILING "Enable gprof profiling (-pg)" OFF)
option(ENABLE_ASAN "Enable AddressSanitizer in Debug builds" OFF)
if(ENABLE_PROFILING)
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -pg")
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -pg")
endif()
# Set flags for Debug builds to include AddressSanitizer and debug symbols (-g)
# These are applied only when ENABLE_ASAN is enabled or when explicitly requested
if(ENABLE_ASAN)
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -g -fsanitize=address")
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -g -fsanitize=address")
set(CMAKE_EXE_LINKER_FLAGS_DEBUG "${CMAKE_EXE_LINKER_FLAGS_DEBUG} -fsanitize=address")
set(CMAKE_SHARED_LINKER_FLAGS_DEBUG "${CMAKE_SHARED_LINKER_FLAGS_DEBUG} -fsanitize=address")
endif()
option(ENABLE_COVERAGE "Build with gcov coverage instrumentation" OFF)
if(ENABLE_COVERAGE AND CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
add_compile_options(--coverage -O0 -g)
add_link_options(--coverage)
endif()
FetchContent_Declare(spline
GIT_REPOSITORY https://github.com/AlfVII/spline.git
GIT_TAG 10b4ca336b8ad3b3b842d4a2548a94ea00355a5f) # pin default-branch HEAD to known-good
FetchContent_MakeAvailable(spline)
include_directories("${CMAKE_BINARY_DIR}/_deps/spline-src/src")
FetchContent_Declare(svg
GIT_REPOSITORY https://github.com/AlfVII/svg
GIT_TAG b92693f84ba6bbe79ea1965b68fffbcd1b991186) # pin default-branch HEAD to known-good
FetchContent_MakeAvailable(svg)
include_directories("${CMAKE_BINARY_DIR}/_deps/svg-src/src")
FetchContent_Declare(rapidfuzz
GIT_REPOSITORY https://github.com/rapidfuzz/rapidfuzz-cpp.git
GIT_TAG b5830af53bd1b3c7460a8de1e9f7095df99b3470) # was 'main' (floating); pinned to known-good
FetchContent_MakeAvailable(rapidfuzz)
# Eigen - Header-only linear algebra library (for Albach 2D boundary value solver)
FetchContent_Declare(eigen
GIT_REPOSITORY https://gitlab.com/libeigen/eigen.git
GIT_TAG 3.4.0
GIT_SHALLOW TRUE)
# Disable Eigen's BLAS/LAPACK and testing (avoids Fortran dependency for WASM builds)
set(EIGEN_BUILD_BLAS OFF CACHE BOOL "" FORCE)
set(EIGEN_BUILD_LAPACK OFF CACHE BOOL "" FORCE)
set(EIGEN_BUILD_TESTING OFF CACHE BOOL "" FORCE)
set(EIGEN_BUILD_DOC OFF CACHE BOOL "" FORCE)
set(BUILD_TESTING OFF CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(eigen)
include_directories("${CMAKE_BINARY_DIR}/_deps/eigen-src")
# ============================================================================
# ngspice - Circuit Simulator (optional)
# ============================================================================
if(ENABLE_NGSPICE)
message(STATUS "Configuring ngspice...")
set(NGSPICE_PREFIX "${CMAKE_BINARY_DIR}/_deps/ngspice")
set(NGSPICE_INSTALL_DIR "${NGSPICE_PREFIX}/install")
# Check if ngspice is already installed (e.g. from a previous build)
set(NGSPICE_ALREADY_INSTALLED FALSE)
if(EXISTS "${NGSPICE_INSTALL_DIR}/lib/libngspice.so" AND EXISTS "${NGSPICE_INSTALL_DIR}/include/ngspice/sharedspice.h")
set(NGSPICE_ALREADY_INSTALLED TRUE)
message(STATUS "ngspice already installed at ${NGSPICE_INSTALL_DIR}, skipping build")
endif()
if(NOT NGSPICE_ALREADY_INSTALLED)
# ngspice can be built as a shared library with the sharedspice API
# We use ExternalProject for more control over the build process
include(ExternalProject)
set(NGSPICE_VERSION "45.2")
set(NGSPICE_URL "https://sourceforge.net/projects/ngspice/files/ng-spice-rework/${NGSPICE_VERSION}/ngspice-${NGSPICE_VERSION}.tar.gz/download")
# Configure options for ngspice shared library build
# Basic configuration - NO CIDER, NO XSPICE for minimal build
# Key options:
# --with-ngshared: Build as shared library with C API
# --disable-debug: Disable debug for performance
# --without-x: No X11 dependencies
if(EMSCRIPTEN)
# Special configuration for WASM/Emscripten build
# Based on ngspice patch #96: https://sourceforge.net/p/ngspice/patches/96/
# Using --with-ngshared produces a static lib (.so extension but actually WASM static lib)
# that provides the sharedspice.h API for WASM targets
set(NGSPICE_CONFIGURE_FLAGS
"--with-ngshared"
"--disable-debug"
"--without-x"
"--disable-xspice"
"--disable-cider"
"--disable-openmp"
"--with-readline=no"
"CFLAGS=-O2 -fwasm-exceptions -sSUPPORT_LONGJMP=wasm"
"CXXFLAGS=-O2 -fwasm-exceptions -sSUPPORT_LONGJMP=wasm"
"LDFLAGS=-fwasm-exceptions -sSUPPORT_LONGJMP=wasm"
)
# Create a patch script to fix WASM compatibility issues
# Based on ngspice patch #96 for WebAssembly support
set(NGSPICE_PATCH_SCRIPT "${NGSPICE_PREFIX}/apply_wasm_patches.sh")
file(WRITE ${NGSPICE_PATCH_SCRIPT} "#!/bin/bash
set -e
cd \$1
# Fix configure to recognize .wasm output files
if [ -f configure ]; then
sed -i 's/ac_files=\\\"a.out/ac_files=\\\"a.out a.out.js a.out.wasm/' configure
fi
# Fix main.c - remove main function when building shared module
# The SHARED_MODULE macro should already handle this in newer ngspice versions
if [ -f src/main.c ]; then
# Check if SHARED_MODULE guard already exists around main
if ! grep -q '#ifndef SHARED_MODULE' src/main.c 2>/dev/null || ! grep -A1 '#ifndef SHARED_MODULE' src/main.c | grep -q 'int main'; then
# Add guard around main function if not present
sed -i 's/^int main(int argc, char \\*\\*argv)/\\n#ifndef SHARED_MODULE\\nint main(int argc, char **argv)/' src/main.c 2>/dev/null || true
# Find the end of main and add endif (this is approximate - newer versions may already have this)
fi
fi
# Fix misc_time.c - getrusage doesn't work properly in WASM
if [ -f src/misc/misc_time.c ]; then
# Add EMSCRIPTEN guard around getrusage usage
sed -i 's/#ifdef HAVE_GETRUSAGE/#if defined(HAVE_GETRUSAGE) \\&\\& !defined(__EMSCRIPTEN__)/' src/misc/misc_time.c 2>/dev/null || true
fi
# Fix sharedspice.c - skip user config file loading for WASM
if [ -f src/sharedspice.c ]; then
# Add EMSCRIPTEN guard to skip config file access
sed -i 's/read_initialisation_file()/#ifndef __EMSCRIPTEN__\\n read_initialisation_file()\\n#endif/' src/sharedspice.c 2>/dev/null || true
fi
# Fix frontend/outitf.c - function signature mismatch
if [ -f src/frontend/outitf.c ]; then
# The sh_vecinit signature fix - search for the function and ensure proper signature
:
fi
echo 'WASM patches applied successfully'
")
ExternalProject_Add(ngspice_external
URL ${NGSPICE_URL}
PREFIX ${NGSPICE_PREFIX}
PATCH_COMMAND bash ${NGSPICE_PATCH_SCRIPT} <SOURCE_DIR>
CONFIGURE_COMMAND emconfigure <SOURCE_DIR>/configure ${NGSPICE_CONFIGURE_FLAGS} --prefix=${NGSPICE_INSTALL_DIR}
BUILD_COMMAND emmake make -j${CMAKE_BUILD_PARALLEL_LEVEL}
INSTALL_COMMAND make install
BUILD_IN_SOURCE 0
# Note: Despite .so extension, this is actually a WASM static library
# Version 45.2 produces libngspice.so.0.0.14
BUILD_BYPRODUCTS ${NGSPICE_INSTALL_DIR}/lib/libngspice.so.0.0.14 ${NGSPICE_INSTALL_DIR}/lib/libngspice.so
)
else()
# Native build configuration - basic ngspice only
set(NGSPICE_CONFIGURE_FLAGS
"--with-ngshared"
"--disable-debug"
"--without-x"
"--disable-xspice"
"--disable-cider"
"--disable-openmp"
"CFLAGS=-O2 -fPIC"
)
ExternalProject_Add(ngspice_external
URL ${NGSPICE_URL}
PREFIX ${NGSPICE_PREFIX}
CONFIGURE_COMMAND <SOURCE_DIR>/configure ${NGSPICE_CONFIGURE_FLAGS} --prefix=${NGSPICE_INSTALL_DIR}
BUILD_COMMAND make -j${CMAKE_BUILD_PARALLEL_LEVEL}
INSTALL_COMMAND make install
BUILD_IN_SOURCE 0
BUILD_BYPRODUCTS ${NGSPICE_INSTALL_DIR}/lib/libngspice.so
)
endif()
endif()
# Create imported target for ngspice
add_library(ngspice INTERFACE)
if(NOT NGSPICE_ALREADY_INSTALLED)
add_dependencies(ngspice ngspice_external)
endif()
# Set include and library paths
target_include_directories(ngspice INTERFACE
${NGSPICE_INSTALL_DIR}/include
${NGSPICE_INSTALL_DIR}/include/ngspice
)
if(WIN32)
target_link_libraries(ngspice INTERFACE ${NGSPICE_INSTALL_DIR}/lib/ngspice.lib)
elseif(EMSCRIPTEN)
# Despite .so extension, this is a WASM static library when built with emscripten
# Use the symlink for version independence
target_link_libraries(ngspice INTERFACE ${NGSPICE_INSTALL_DIR}/lib/libngspice.so)
else()
target_link_libraries(ngspice INTERFACE ${NGSPICE_INSTALL_DIR}/lib/libngspice.so)
endif()
# Add compile definition
add_compile_definitions(ENABLE_NGSPICE)
endif()
# Testing framework - Catch2
if(INCLUDE_MKF_TESTS)
FetchContent_Declare(Catch2
GIT_REPOSITORY https://github.com/catchorg/Catch2.git
GIT_TAG v3.8.1)
FetchContent_MakeAvailable(Catch2)
list(APPEND CMAKE_MODULE_PATH ${catch2_SOURCE_DIR}/extras)
include(CTest)
include(Catch)
endif()
# MAS
find_program(NAMES quicktype REQUIRED)
find_program(NAMES libgtest-dev REQUIRED)
if(NOT DEFINED MAS_DIRECTORY)
set(MAS_DIRECTORY "${CMAKE_BINARY_DIR}/MAS/")
endif(NOT DEFINED MAS_DIRECTORY)
if(NOT DEFINED MAS_DIR)
set(MAS_DIR "${PROJECT_SOURCE_DIR}/MAS")
endif(NOT DEFINED MAS_DIR)
file(MAKE_DIRECTORY "${CMAKE_BINARY_DIR}/MAS")
add_custom_command(
OUTPUT "${MAS_DIRECTORY}/MAS.hpp"
COMMAND quicktype -l c++ -s schema ${MAS_DIR}/schemas/MAS.json
-S ${MAS_DIR}/schemas/magnetic.json
-S ${MAS_DIR}/schemas/magnetic/core.json
-S ${MAS_DIR}/schemas/magnetic/coil.json
-S ${MAS_DIR}/schemas/utils.json
-S ${MAS_DIR}/schemas/magnetic/core/gap.json
-S ${MAS_DIR}/schemas/magnetic/core/shape.json
-S ${MAS_DIR}/schemas/magnetic/core/material.json
-S ${MAS_DIR}/schemas/magnetic/core/coating.json
-S ${MAS_DIR}/schemas/magnetic/insulation/material.json
-S ${MAS_DIR}/schemas/magnetic/insulation/wireCoating.json
-S ${MAS_DIR}/schemas/magnetic/bobbin.json
-S ${MAS_DIR}/schemas/magnetic/core/piece.json
-S ${MAS_DIR}/schemas/magnetic/core/spacer.json
-S ${MAS_DIR}/schemas/magnetic/wire/basicWire.json
-S ${MAS_DIR}/schemas/magnetic/wire/round.json
-S ${MAS_DIR}/schemas/magnetic/wire/rectangular.json
-S ${MAS_DIR}/schemas/magnetic/wire/foil.json
-S ${MAS_DIR}/schemas/magnetic/wire/planar.json
-S ${MAS_DIR}/schemas/magnetic/wire/litz.json
-S ${MAS_DIR}/schemas/magnetic/wire/material.json
-S ${MAS_DIR}/schemas/magnetic/wire.json
-S ${MAS_DIR}/schemas/utils.json
-S ${MAS_DIR}/schemas/magnetic/insulation/wireCoating.json
-S ${MAS_DIR}/schemas/magnetic/insulation/material.json
-S ${MAS_DIR}/schemas/inputs.json
-S ${MAS_DIR}/schemas/outputs.json
-S ${MAS_DIR}/schemas/outputs/coreLossesOutput.json
-S ${MAS_DIR}/schemas/inputs/designRequirements.json
-S ${MAS_DIR}/schemas/inputs/operatingConditions.json
-S ${MAS_DIR}/schemas/inputs/operatingPoint.json
-S ${MAS_DIR}/schemas/inputs/operatingPointExcitation.json
-S ${MAS_DIR}/schemas/inputs/topologies/flyback.json
-S ${MAS_DIR}/schemas/inputs/topologies/currentTransformer.json
-S ${MAS_DIR}/schemas/inputs/topologies/boost.json
-S ${MAS_DIR}/schemas/inputs/topologies/buck.json
-S ${MAS_DIR}/schemas/inputs/topologies/forward.json
-S ${MAS_DIR}/schemas/inputs/topologies/isolatedBuck.json
-S ${MAS_DIR}/schemas/inputs/topologies/isolatedBuckBoost.json
-S ${MAS_DIR}/schemas/inputs/topologies/pushPull.json
-S ${MAS_DIR}/schemas/inputs/topologies/cllcResonant.json
-S ${MAS_DIR}/schemas/inputs/topologies/dualActiveBridge.json
-S ${MAS_DIR}/schemas/inputs/topologies/llcResonant.json
-S ${MAS_DIR}/schemas/inputs/topologies/phaseShiftedFullBridge.json
-S ${MAS_DIR}/schemas/inputs/topologies/phaseShiftedHalfBridge.json
-S ${MAS_DIR}/schemas/inputs/topologies/asymmetricHalfBridge.json
-S ${MAS_DIR}/schemas/inputs/topologies/cuk.json
-S ${MAS_DIR}/schemas/inputs/topologies/clllcResonant.json
-S ${MAS_DIR}/schemas/inputs/topologies/powerFactorCorrection.json
-S ${MAS_DIR}/schemas/inputs/topologies/commonModeChoke.json
-S ${MAS_DIR}/schemas/inputs/topologies/differentialModeChoke.json
-S ${MAS_DIR}/schemas/inputs/topologies/sepic.json
-S ${MAS_DIR}/schemas/inputs/topologies/zeta.json
-S ${MAS_DIR}/schemas/inputs/topologies/weinberg.json
-S ${MAS_DIR}/schemas/inputs/topologies/fourSwitchBuckBoost.json
-S ${MAS_DIR}/schemas/inputs/topologies/seriesResonant.json
-S ${MAS_DIR}/schemas/inputs/topologies/vienna.json
-o ${MAS_DIRECTORY}/MAS.hpp --namespace MAS --source-style single-source --type-style pascal-case --member-style underscore-case --enumerator-style upper-underscore-case --no-boost
USES_TERMINAL)
add_custom_target(MAS ALL
DEPENDS "${MAS_DIRECTORY}/MAS.hpp")
include_directories("${MAS_DIRECTORY}")
# CAS
if(NOT DEFINED CAS_DIRECTORY)
set(CAS_DIRECTORY "${CMAKE_BINARY_DIR}/CAS/")
endif()
if(NOT DEFINED CAS_DIR)
set(CAS_DIR "${PROJECT_SOURCE_DIR}/CAS")
endif()
if(NOT DEFINED PEAS_DIR)
set(PEAS_DIR "${PROJECT_SOURCE_DIR}/PEAS")
endif()
file(MAKE_DIRECTORY "${CMAKE_BINARY_DIR}/CAS")
add_custom_command(
OUTPUT "${CAS_DIRECTORY}/CAS.hpp"
COMMAND quicktype -l c++ -s schema ${CAS_DIR}/schemas/inputs.json
-S ${CAS_DIR}/schemas/inputs/designRequirements.json
-S ${PEAS_DIR}/schemas/utils.json
-S ${PEAS_DIR}/schemas/inputs/twoTerminalOperatingPoint.json
-S ${PEAS_DIR}/schemas/inputs/operatingConditions.json
-S ${PEAS_DIR}/schemas/inputs/operatingPointExcitation.json
-o ${CAS_DIRECTORY}/CAS.hpp --namespace CAS --source-style single-source
--type-style pascal-case --member-style underscore-case
--enumerator-style upper-underscore-case --no-boost
--top-level Inputs
DEPENDS
"${CAS_DIR}/schemas/inputs.json"
"${CAS_DIR}/schemas/inputs/designRequirements.json"
"${PEAS_DIR}/schemas/utils.json"
"${PEAS_DIR}/schemas/inputs/twoTerminalOperatingPoint.json"
"${PEAS_DIR}/schemas/inputs/operatingConditions.json"
"${PEAS_DIR}/schemas/inputs/operatingPointExcitation.json"
USES_TERMINAL)
add_custom_target(CAS ALL DEPENDS "${CAS_DIRECTORY}/CAS.hpp")
add_dependencies(CAS MAS)
include_directories("${CAS_DIRECTORY}")
# ============================================================================
# CCI strand coordinates — embed up to N=1000 into the binary at build time
# ============================================================================
set(CCI_COORDS_DIR "${CMAKE_CURRENT_SOURCE_DIR}/cci_coords"
CACHE PATH "Path to cci_coords repository (submodule at MKF/cci_coords)")
set(CCI_GENERATED_CPP "${CMAKE_BINARY_DIR}/generated/CciCoordinatesData.cpp")
find_package(Python3 REQUIRED COMPONENTS Interpreter)
add_custom_command(
OUTPUT "${CCI_GENERATED_CPP}"
COMMAND "${Python3_EXECUTABLE}"
"${CMAKE_CURRENT_SOURCE_DIR}/scripts/generate_cci_data.py"
"${CCI_COORDS_DIR}"
"${CCI_GENERATED_CPP}"
DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/scripts/generate_cci_data.py"
COMMENT "Generating embedded CCI coordinates"
VERBATIM)
add_custom_target(cci_data_gen DEPENDS "${CCI_GENERATED_CPP}")
file(GLOB SOURCES CONFIGURE_DEPENDS
"src/*.cpp"
"src/advisers/*.cpp"
"src/constructive_models/*.cpp"
"src/converter_models/*.cpp"
"src/physical_models/*.cpp"
"src/processors/*.cpp"
"src/support/*.cpp")
list(APPEND SOURCES "${CCI_GENERATED_CPP}")
SET(LIBS data::insulation_standards)
# REMOVED: LINK_DIRECTORIES for LAPACKBLAS (no longer needed, levmar replaced by Eigen)
# libraries the demo depends on
IF(HAVE_PLASMA)
LINK_DIRECTORIES(${PLASMA_DIR}/lib)
SET(LIBS ${LIBS} ${PLASMA_LIB_NAMES})
ENDIF(HAVE_PLASMA)
# On Windows, build as STATIC to avoid DLL boundary issues with inline globals
# On other platforms, build as SHARED for better compatibility
if(WIN32)
add_library(MKF STATIC ${SOURCES})
else()
add_library(MKF SHARED ${SOURCES})
endif()
set_property(TARGET MKF PROPERTY POSITION_INDEPENDENT_CODE ON)
add_dependencies(MKF MAS CAS cci_data_gen)
target_include_directories(MKF PUBLIC "${CMAKE_BINARY_DIR}/generated")
cmrc_add_resource_library(insulation_standards ALIAS data::insulation_standards NAMESPACE insulationData src/data/insulation_standards/IEC_60664-1.json src/data/insulation_standards/IEC_60664-4.json src/data/insulation_standards/IEC_60664-5.json src/data/insulation_standards/IEC_62368-1.json src/data/insulation_standards/IEC_61558-1.json src/data/insulation_standards/IEC_61558-2-16.json src/data/insulation_standards/IEC_60335-1.json)
cmrc_add_resource_library(core_losses_data ALIAS data::core_losses_data NAMESPACE coreLossesData src/data/core_losses/ciGSE_coefficients.json)
target_link_libraries(MKF PUBLIC ${LIBS})
set(DATA_LIST "")
if(EMBED_MAS_DATA)
if(EMBED_MAS_CORES)
list (APPEND DATA_LIST MAS/data/cores_stock.ndjson)
list (APPEND DATA_LIST MAS/data/cores.ndjson)
endif()
if(EMBED_MAS_CORE_SHAPES)
list (APPEND DATA_LIST MAS/data/core_shapes.ndjson)
endif()
if(EMBED_MAS_CORE_MATERIALS)
list (APPEND DATA_LIST MAS/data/core_materials.ndjson)
endif()
if(EMBED_MAS_WIRES)
list (APPEND DATA_LIST MAS/data/wires.ndjson)
endif()
if(EMBED_MAS_WIRE_MATERIALS)
list (APPEND DATA_LIST MAS/data/wire_materials.ndjson)
endif()
if(EMBED_MAS_BOBBINS)
list (APPEND DATA_LIST MAS/data/bobbins.ndjson)
endif()
if(EMBED_MAS_INSULATION_MATERIALS)
list (APPEND DATA_LIST MAS/data/insulation_materials.ndjson)
endif()
endif()
message(${DATA_LIST})
cmrc_add_resource_library(data ALIAS data::data NAMESPACE data ${DATA_LIST})
target_link_libraries(MKF PUBLIC data::data)
target_link_libraries(MKF PUBLIC data::core_losses_data)
target_link_libraries(MKF PUBLIC rapidfuzz::rapidfuzz)
target_link_libraries(MKF PRIVATE nlohmann_json::nlohmann_json)
# Link ngspice if enabled
if(ENABLE_NGSPICE)
target_link_libraries(MKF PUBLIC ngspice)
if(NOT NGSPICE_ALREADY_INSTALLED)
add_dependencies(MKF ngspice_external)
endif()
endif()
target_include_directories(MKF PUBLIC
"${PROJECT_SOURCE_DIR}/src/"
"${PROJECT_SOURCE_DIR}/src/advisers/"
"${PROJECT_SOURCE_DIR}/src/constructive_models/"
"${PROJECT_SOURCE_DIR}/src/converter_models/"
"${PROJECT_SOURCE_DIR}/src/physical_models/"
"${PROJECT_SOURCE_DIR}/src/processors/"
"${PROJECT_SOURCE_DIR}/src/support/"
"${CMAKE_BINARY_DIR}/_deps/eigen-src"
)
# add_executable(magnetic_adviser "src/advisers/MagneticAdviser.cpp")
# target_link_libraries(magnetic_adviser MKF)
if(INCLUDE_MKF_TESTS)
file(GLOB ALL_TEST_SOURCES CONFIGURE_DEPENDS "tests/*.cpp")
# TestingUtils.cpp holds shared fixtures; explicitly added below.
list(FILTER ALL_TEST_SOURCES EXCLUDE REGEX "TestingUtils\\.cpp$")
add_executable(MKF_tests ${ALL_TEST_SOURCES} "tests/TestingUtils.cpp")
target_link_libraries(MKF_tests PRIVATE Catch2::Catch2WithMain)
target_link_libraries(MKF_tests PUBLIC MKF)
target_include_directories(MKF_tests PUBLIC "${PROJECT_SOURCE_DIR}/src/")
# Precompiled header for the test target.
#
# The TestTopology*.cpp files (3k LOC each, 10+ of them) each parse the
# generated MAS.hpp (~700KB of template-heavy quicktype output), Catch2's
# header-only `catch_all.hpp`, nlohmann/json, MKF's own constructive +
# converter model headers — every TU re-parses and re-instantiates the
# same template machinery. Pre-compiling once amortises the cost across
# the whole test build: empirically ~5-10x faster compile on the heavy
# TestTopology* files with ZERO runtime impact (PCH is a parser-state
# cache, not a code change — the generated object files are identical).
target_precompile_headers(MKF_tests PRIVATE
<catch2/catch_all.hpp>
<nlohmann/json.hpp>
<MAS.hpp>
<vector>
<string>
<optional>
<memory>
<map>
<variant>
)
# Discover and register tests with CTest
catch_discover_tests(MKF_tests)
endif(INCLUDE_MKF_TESTS)