Skip to content

Conversation

JCGoran
Copy link
Collaborator

@JCGoran JCGoran commented Jun 4, 2025

Split off from #2746 since that PR was getting a bit too large.

Context

Currently we build mechanisms (mod files) using a hand-written Makefile. As a result, we need to ship a bunch of Make-specific tools with NEURON. This also does not work (or at least, not without a lot of workarounds) in a cross-platform manner.

This PR allows one to build NEURON mechanisms with CMake, using any generator we want. The way it achieves this is by exposing the create_nrnmech CMake function, which works as follows:

cmake_minimum_required(VERSION 3.15)
project(modfile_test)

find_package(neuron REQUIRED)

create_nrnmech(NEURON CORENEURON SPECIAL MOD_FILES
  modfile1.mod
  path/to/modfile2.mod)

Note that the NEURON, CORENEURON, and SPECIAL toggle whether one builds libnrnmech, libcorenrnmech, and the special executable (enabling CORENEURON and SPECIAL together enables the special-core executable as well).

One can then build the targets via:

cmake -B build
cmake --build build

which can then be loaded into NEURON as usual:

nrniv -dll build/libnrnmech.so

Additional notes

  • the NEURON config file must be visible to CMake, i.e. it must be added to CMAKE_PREFIX_PATH, and the nmodl executable must be in PATH nmodl is now installed as a NEURON target so we only need CMAKE_PREFIX_PATH. Additionally, until NOCMODL fails to run from build directory without setting MODLUNIT #3470 is resolved, MODLUNIT must also be set issue fixed!
  • at build-time, internal mechanisms are still built with make, but this can be fixed later (nrnivmodl touches a lot of the internal code as well, so the changeset would be much larger)
  • when shipping wheels, the CMAKE_PREFIX_PATH should include {NEURON_WHEEL_DIR}/.data/lib/cmake, though I am not sure how to obtain it without running any external utilities
  • CMake and any of the generators should be available on the user machine. We could update the Python requirements to include cmake and ninja, but since this feature is a bit experimental, I'm not sure how useful it is
  • I've not tested anything on Windows, so I'm not sure how cross-platform it really is (i.e. we ship NEURON with msys2 or whatever else is there, but CMake would probably try to use MSVC if it's installed)
  • for NVHPC, we are hardcoding some of the CLI options for now, which means that one should build NEURON from source, and then use the same installation for building mechanisms (otherwise I cannot guarantee that the mechanism-building will work) looks like it works without too much hardcoding
  • running special when building from a wheel currently does not work OOTB because of the fact that NEURON hardcodes the path to nrnpyenv.sh. The workaround is to add NEURONHOME=${NEURON_WHEEL_DIR}/.data/share/nrn" to the env added the same workaround as we use for wheels so now it works
  • I manually tried testing special-core with NVHPC 25.3 and it works without issues. I did not want to add too many new tests since that will be tested anyway once the CMake build itself starts using create_nrnmech instead of calling nrnivmodl (that's planned for part 2)
  • since this PR introduces a public API, I documented it here

matz-e and others added 21 commits August 14, 2024 16:03
This PR aims to allow dependent projects to build their models via CMake
rather than a combination of shell scripts and Makefiles.  In the long
term, this may help facilitate building natively on Windows.

To try:
```
git clone -b imported-main https://github.com/BlueBrain/neurodamus-models.git
cd neurodamus-models
```

Create a `CMakeLists.txt` with contents like:
```cmake
cmake_minimum_required(VERSION 3.28)

project(newrodamus)

find_package(neuron REQUIRED)

create_libnrnmech(MOD_FILES
  neocortex/mod/v6/CaDynamics_DC0.mod
  neocortex/mod/v6/Ca_HVA2.mod
  neocortex/mod/v6/Ca_LVAst.mod
  neocortex/mod/v6/DetAMPANMDA.mod
  neocortex/mod/v6/DetGABAAB.mod
  neocortex/mod/v6/GluSynapse.mod
  neocortex/mod/v6/Ih.mod
  neocortex/mod/v6/K_Pst.mod
  neocortex/mod/v6/K_Tst.mod
  neocortex/mod/v6/KdShu2007.mod
  neocortex/mod/v6/NaTg.mod
  neocortex/mod/v6/Nap_Et2.mod
  neocortex/mod/v6/ProbAMPANMDA_EMS.mod
  neocortex/mod/v6/ProbGABAAB_EMS.mod
  neocortex/mod/v6/SK_E2.mod
  neocortex/mod/v6/SKv3_1.mod
  neocortex/mod/v6/StochKv3.mod
  neocortex/mod/v6/TTXDynamicsSwitch.mod
  neocortex/mod/v6/VecStim.mod
  neocortex/mod/v6/gap.mod
  neocortex/mod/v6/netstim_inhpoisson.mod
)
```

Then build and install:
```
cmake -B build -S . -GNinja -DCMAKE_INSTALL_PREFIX=x86_64
cmake --build build
cmake --install build
```

To be continued‥
Always use resolved absolute paths in CMake since nocmodl has some
trouble with symlinks.
* add docs and various args
Also finally add (hacky) test
Copy link
Contributor

github-actions bot commented Jun 4, 2025

✔️ 535bb1c -> artifacts URL

Copy link

✔️ 535bb1c -> Azure artifacts URL

Copy link

✔️ 3a7bb98 -> Azure artifacts URL

Copy link
Contributor

github-actions bot commented Jun 4, 2025

✔️ 3a7bb98 -> artifacts URL

Copy link
Contributor

github-actions bot commented Jun 4, 2025

✔️ 908dc4e -> artifacts URL

Copy link

✔️ 908dc4e -> Azure artifacts URL

Copy link
Contributor

✔️ 73f346c -> artifacts URL

Copy link
Contributor

✔️ 64963f9 -> artifacts URL

Copy link

✔️ 73f346c -> Azure artifacts URL

Copy link
Contributor

✔️ 5f5f413 -> artifacts URL

Copy link

✔️ 5f5f413 -> Azure artifacts URL

@JCGoran JCGoran marked this pull request as ready for review June 20, 2025 10:49
Copy link
Contributor

✔️ 66ba781 -> artifacts URL

@JCGoran JCGoran requested a review from nrnhines June 20, 2025 10:54
Copy link

✔️ 66ba781 -> Azure artifacts URL

Copy link
Contributor

✔️ 67c2759 -> artifacts URL

Copy link

✔️ 67c2759 -> Azure artifacts URL

Copy link

sonarqubecloud bot commented Jul 8, 2025

Copy link

✔️ 36887b0 -> Azure artifacts URL

Copy link
Contributor

✔️ 36887b0 -> artifacts URL

Copy link

sonarqubecloud bot commented Sep 9, 2025

Copy link
Contributor

github-actions bot commented Sep 9, 2025

✔️ 697e1d2 -> artifacts URL

Copy link

✔️ 697e1d2 -> Azure artifacts URL

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants