Skip to content

Commit

Permalink
Updates for Windows MSVC build
Browse files Browse the repository at this point in the history
In setup.py and CMakeLists.txt for build

- Added switches for compiler flags for Windows/MSVC

- Removed possible override of version in CMakeLists.txt: setup.py
  should be the only source of version info

- Archive format set to zip for windows

- Using module name moose instead of _moose, the underscore is added
  as PREFIX

- Platform dependent shared lib file name extension

- Removed platform name CMAKE from cmake file

- Updated setup.py to allow release build on windows

In C++ code

- Switched all new style logical opeartors (and, or, not)to C
  style (&&, ||, !) - these are not recognized by MSVC compiler

- Added windows port of getopt in external

- Removed SocketStreamer conditionally for Win32 build.
  Win32 does not have socket library. TODO: look at ports.

- CylBase uses a constant PI which does not exist in win32 standard
  library. Replaced it with cmath's M_PI. Hope they are the same.

- Corrected cnpy function for checking numpy file header

- Minor correction on printf formatting to avoid warning for size_t
build updates for windows

- Undefine _DEBUG macro for release builds

Added build instructions for windows
  • Loading branch information
subhacom committed Jun 12, 2024
1 parent 4268e40 commit 6d0b9e8
Show file tree
Hide file tree
Showing 47 changed files with 440 additions and 335 deletions.
85 changes: 71 additions & 14 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,30 +1,33 @@
cmake_minimum_required(VERSION 3.20 FATAL_ERROR)

set( CMAKE_VERBOSE_MAKEFILE on )

# Project to build MOOSE's python module.
project(PyMOOSE)

# cmake related macros.
set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
include(CheckCXXCompiler.cmake)
include(CheckIncludeFileCXX)
# include(CheckIncludeFileCXX)


# We find python executable here. Though mainly used inside pymoose.
find_package(Python3 COMPONENTS Interpreter Numpy)
set(PYTHON_EXECUTABLE ${Python3_EXECUTABLE})
find_package(PythonInterp 3.8)
find_package(Python COMPONENTS Interpreter Development NumPy)

set(CMAKE_MACOSX_RPATH OFF)

# NOTE: version should be changed in setup.py file.
# If moose version is not given, use setup.py file to get the default version.
if(NOT VERSION_MOOSE)
execute_process(COMMAND ${PYTHON_EXECUTABLE} setup.py --version
# Note: Sun Jun 9 12:12:28 IST 2024 - somehow build does not get the version - it remains empty when creating packages and the process fails with file not found error:
# - MOOSE_VERSION is a preprocessor macro to keep the version number as part of the binary library (Shell.cpp uses it) whereas VERSION_MOOSE is the CMake variable passed for populating the preprocessor macro def as well as for adding version info in the package file name.
execute_process(COMMAND ${PYTHON_EXECUTABLE} setup.py --version
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
OUTPUT_VARIABLE VERSION_MOOSE
OUTPUT_STRIP_TRAILING_WHITESPACE)
endif()


add_definitions(-DMOOSE_VERSION="${VERSION_MOOSE}")


message(STATUS "MOOSE Version ${VERSION_MOOSE}")

# This snippet is from LLVM project.
Expand Down Expand Up @@ -65,6 +68,17 @@ option(WITH_LEGACY_BINDING "Use legacy python-bindings" OFF)

# Default definitions.
add_definitions(-DUSE_GENESIS_PARSER)

# M_PI is undefined in MSVC unless _USE_MATH_DEFINES is defined before cmath import
# Also, MSVC throws this error
# "external\exprtk\exprtk.hpp(10183): fatal error C1128: number of sections exceeded object file format limit"
# and suggests "compile with /bigobj"
# - Subha
if(MSVC)
add_definitions(-D_USE_MATH_DEFINES)
set(CMAKE_CXX_COMPILE_FLAGS ${CMAKE_CXX_COMPILE_FLAGS} /bigobj)
endif()

if(DEBUG OR "${CMAKE_BUILD_TYPE}" STREQUAL "Debug")
message(STATUS "Building for Debug/Unit testing")
add_definitions(-DDO_UNIT_TESTS -O)
Expand All @@ -76,7 +90,9 @@ elseif(ENABLE_UNIT_TESTS)
else()
message(STATUS "Building for Release/No unit tests.")
set(CMAKE_BUILD_TYPE Release)
add_definitions(-UDO_UNIT_TESTS -O3 -DDISABLE_DEBUG)
if(NOT MSVC)
add_definitions(-UDO_UNIT_TESTS -O3 -DDISABLE_DEBUG)
endif()
endif()

if(GPROF AND "${CMAKE_BUILD_TYPE}" STREQUAL "Debug")
Expand All @@ -94,6 +110,13 @@ if(WITH_ASAN AND "${CMAKE_BUILD_TYPE}" STREQUAL "Debug")
set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} -fsanitize=address")
endif()

if(MSVC)
set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} /FORCE:MULTIPLE")
set(CMAKE_LINKER_FLAGS "${CMAKE_LINKER_FLAGS} /FORCE:MULTIPLE")
set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} /FORCE:MULTIPLE")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /FORCE:MULTIPLE")
endif()

# Override default GSL solvers when BOOST is enabled.
if(WITH_BOOST OR WITH_BOOST_ODE)
set(WITH_BOOST_ODE ON)
Expand Down Expand Up @@ -122,15 +145,15 @@ endif()
################################### TARGETS ####################################

link_directories(${CMAKE_BINARY_DIR})
add_library(libmoose SHARED basecode/main.cpp)
add_library(libmoose SHARED basecode/main.cpp external/getopt/getopt.c)
set_target_properties(libmoose PROPERTIES PREFIX "")
add_executable(moose.bin basecode/main.cpp)


################################### SETUP BUILD ################################

# Variable to collect all static libraries.
set(STATIC_LIBRARIES "" )
set(STATIC_LIBRARIES "")
# Collect all shared libraries here.
set(SYSTEM_SHARED_LIBS "")

Expand Down Expand Up @@ -218,6 +241,12 @@ if(WITH_MPI)
endif()
endif(WITH_MPI)


# DEBUG compilation process
# get_target_property(compile_defs libmoose COMPILE_DEFINITIONS)
# message(STATUS "App compile definitions are ${compile_defs}")
# DEBUG compilation process

# Add subdirectroeis
add_subdirectory(basecode)
add_subdirectory(msg)
Expand All @@ -241,6 +270,7 @@ add_subdirectory(external)

# development related.
add_subdirectory(devel)


###################################### LINKING #################################
list(APPEND MOOSE_LIBRARIES
Expand All @@ -265,6 +295,13 @@ list(APPEND MOOSE_LIBRARIES
basecode
)

# If windows MSVC, include port of getopt
if(MSVC)
list(APPEND MOOSE_LIBRARIES getopt)
target_include_directories(moose.bin PRIVATE external/getopt)
target_include_directories(libmoose PRIVATE external/getopt)
endif()

# Make sure to remove duplicates.
list(REMOVE_DUPLICATES STATIC_LIBRARIES)
if(SYSTEM_SHARED_LIBS)
Expand Down Expand Up @@ -293,6 +330,12 @@ if(APPLE)
${SYSTEM_SHARED_LIBS}
${CMAKE_DL_LIBS}
)
elseif(MSVC)
target_link_libraries(libmoose
${MOOSE_LIBRARIES}
${STATIC_LIBRARIES}
${SYSTEM_SHARED_LIBS}
)
else(APPLE)
target_link_libraries(libmoose
"-Wl,--whole-archive"
Expand All @@ -310,13 +353,16 @@ if( WITH_BOOST )
target_link_libraries( moose.bin ${Boost_LIBRARIES} )
endif( WITH_BOOST )


######################### BUILD PYMOOSE ########################################

# pybind11 should be installed as a dependency.
# It can be easily done with `pip install pybind11`
# See: https://pybind11.readthedocs.io/en/stable/installing.html
# - Subha, Mon Apr 22 14:26:58 IST 2024
execute_process(COMMAND ${CMAKE_COMMAND} -E pybind11-config --cmakedir OUTPUT_VARIABLE pybind11_DIR OUTPUT_STRIP_TRAILING_WHITESPACE)

# The execute_process fails on Windows with Mamba
# execute_process(COMMAND ${CMAKE_COMMAND} -E pybind11-config --cmakedir OUTPUT_VARIABLE pybind11_DIR OUTPUT_STRIP_TRAILING_WHITESPACE)
find_package(pybind11 REQUIRED HINTS "${Python3_SITELIB}")
add_subdirectory(pybind11)

Expand Down Expand Up @@ -364,9 +410,20 @@ include(CTest)
add_subdirectory(tests)

########################### RELEASE #########################################
set(PYMOOSE_SDIST_FILE ${CMAKE_BINARY_DIR}/pymoose-${VERSION_MOOSE}.tar.gz)
if(WIN32)
set(PYMOOSE_SDIST_FILE "${CMAKE_BINARY_DIR}/pymoose-${VERSION_MOOSE}.zip")
set(ARCHIVE_FORMAT zip)
else(WIN32)
set(PYMOOSE_SDIST_FILE "${CMAKE_BINARY_DIR}/pymoose-${VERSION_MOOSE}.tar.gz")
set(ARCHIVE_FORMAT gztar)
endif()

# Tue Jun 11 08:11:00 IST 2024
# NOTE: Calling setup.py directly for building packages is deprecated.
# Use python -m build --wheel instead
# - Subha
add_custom_target(sdist
COMMAND ${PYTHON_EXECUTABLE} setup.py sdist --formats=gztar
COMMAND ${PYTHON_EXECUTABLE} setup.py sdist --formats=${ARCHIVE_FORMAT}
-d ${CMAKE_BINARY_DIR}
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
COMMENT "Creating sdist ${PYMOOSE_SDIST_FILE}"
Expand Down
64 changes: 0 additions & 64 deletions CheckCXXCompiler.cmake

This file was deleted.

1 change: 0 additions & 1 deletion MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
include AUTHORS
include CMakeLists.txt
include CONTRIBUTING.md
include CheckCXXCompiler.cmake
include INSTALL.md
include LICENSE
include MANIFEST.in
Expand Down
12 changes: 11 additions & 1 deletion basecode/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
cmake_minimum_required(VERSION 3.20)
include( ${CMAKE_CURRENT_SOURCE_DIR}/../CheckCXXCompiler.cmake )
add_library(basecode
Element.cpp
DataElement.cpp
Expand All @@ -26,8 +25,19 @@ add_library(basecode
testAsync.cpp
)

# Workaround for getopt included via unistd.h on Unices
if(WIN32)
target_include_directories(basecode PRIVATE ../extern/getopt)
target_link_libraries(basecode PRIVATE getopt)
endif()

add_executable(test_globals testGlobals.cpp global.cpp)

enable_testing()
add_test(NAME cpp_test_globals COMMAND $<TARGET_FILE:test_globals>)

# DEBUG compilation process
get_target_property(compile_defs basecode COMPILE_DEFINITIONS)
message(STATUS "App compile definitions are ${compile_defs}")
# DEBUG compilation process

2 changes: 1 addition & 1 deletion basecode/Conv.h
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,7 @@ template<> class Conv< bool >
}

static void val2str( string& s, bool val ) {
if ( val > 0.5 )
if ( val )
s = "1";
else
s = "0";
Expand Down
4 changes: 2 additions & 2 deletions basecode/doubleEq.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@
** See the file COPYING.LIB for the full notice.
**********************************************************************/

bool doubleEq( double x, double y );
bool doubleApprox( double x, double y );
extern bool doubleEq( double x, double y );
extern bool doubleApprox( double x, double y );
4 changes: 4 additions & 0 deletions basecode/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@
#include <time.h>
#include <math.h>
#include <queue>
#if defined(WIN32)
#include "getopt.h"
#else
#include <unistd.h> // for getopt
#endif
#include "../scheduling/Clock.h"
#include "../msg/DiagonalMsg.h"
#include "../msg/SparseMsg.h"
Expand Down
1 change: 0 additions & 1 deletion biophysics/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
cmake_minimum_required(VERSION 3.20)
include(${CMAKE_CURRENT_SOURCE_DIR}/../CheckCXXCompiler.cmake)

if(WITH_GSL)
find_package(GSL 1.16)
Expand Down
23 changes: 20 additions & 3 deletions builtins/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
cmake_minimum_required(VERSION 3.20)
include( ${CMAKE_CURRENT_SOURCE_DIR}/../CheckCXXCompiler.cmake )

if(MSVC)
add_compile_options("/bigobj")
endif()


# NSDF5 support. Disabled by default.
if(WITH_NSDF)
Expand Down Expand Up @@ -48,6 +52,7 @@ if(WITH_NSDF)
endif( HDF5_FOUND )
endif(WITH_NSDF)

# SocketStreamer.cpp is not portable - so adding conditionally. - Subha
set(SRCS
Arith.cpp
Group.cpp
Expand All @@ -66,10 +71,15 @@ set(SRCS
Interpol2D.cpp
SpikeStats.cpp
MooseParser.cpp
SocketStreamer.cpp
testBuiltins.cpp
)

if(NOT WIN32)
set(SRCS ${SRCS} SocketStreamer.cpp)
else() # MSVC Build system throws unresolved external symbol error
list(APPEND SRCS ../utility/cnpy.cpp)
endif()

if(WITH_NSDF AND HDF5_FOUND)
list(APPEND SRCS
HDF5WriterBase.cpp
Expand All @@ -82,7 +92,14 @@ if(WITH_NSDF AND HDF5_FOUND)
)
endif()

add_library(moose_builtins ${SRCS} )

add_library(moose_builtins ${SRCS})
if(WITH_NSDF AND HDF5_FOUND)
target_link_libraries(moose_builtins ${HDF5_CXX_LIBRARIES} ${HDF5_HL_LIBRARIES})
endif()

# DEBUG compilation process
get_target_property(compile_defs moose_builtins COMPILE_DEFINITIONS)
message(STATUS "App compile definitions are ${compile_defs}")
# DEBUG compilation process

2 changes: 1 addition & 1 deletion builtins/StreamerBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ void StreamerBase::writeToOutFile( const string& filepath
OpenMode m = (openmode == WRITE)?WRITE_BIN:APPEND_BIN;
writeToNPYFile( filepath, m, data, columns );
}
else if( "csv" == outputFormat or "dat" == outputFormat )
else if( "csv" == outputFormat || "dat" == outputFormat )
{
OpenMode m = (openmode == WRITE)?WRITE_STR:APPEND_STR;
writeToCSVFile( filepath, m, data, columns );
Expand Down
Loading

0 comments on commit 6d0b9e8

Please sign in to comment.