Build internal muparser more simply
[alexxy/gromacs.git] / cmake / gmxManageMuparser.cmake
index c4b970332e21241cde7dc1e170238f0cad577c19..0b26a205f7527b60c785e2836f7e5be4229edaad 100644 (file)
@@ -48,31 +48,27 @@ mark_as_advanced(GMX_USE_MUPARSER)
 # linking to work.
 function(gmx_manage_muparser)
     if(GMX_USE_MUPARSER STREQUAL "INTERNAL")
-        # Create an object library for the muparser sources
-        set(BUNDLED_MUPARSER_DIR "${CMAKE_SOURCE_DIR}/src/external/muparser")
-        file(GLOB MUPARSER_SOURCES ${BUNDLED_MUPARSER_DIR}/*.cpp)
-        add_library(muparser_objlib OBJECT ${MUPARSER_SOURCES})
-        # Ensure that the objects can be used in both STATIC and SHARED
-        # libraries.
-        set_target_properties(muparser_objlib PROPERTIES POSITION_INDEPENDENT_CODE ON)
+        # Use cmake's FetchContent to organize the build, even though
+        # the content is already present in src/external. In
+        # particular, it sets up an easy call to add_subdirectory().
+        include(FetchContent)
+        FetchContent_Declare(muparser SOURCE_DIR ${CMAKE_SOURCE_DIR}/src/external/muparser)
+        if (NOT ${muparser}_POPULATED)
+            if (OpenMP_CXX_FLAGS)
+                set(OpenMP_FIND_QUIETLY ON)
+            endif()
+            FetchContent_Populate(muparser)
+        endif()
+        add_subdirectory(${muparser_SOURCE_DIR} ${muparser_BINARY_DIR})
+        if (BUILD_SHARED_LIBS)
+            # Ensure muparser is in the export set called libgromacs,
+            # so that it gets installed along with libgromacs.
+            install(TARGETS muparser EXPORT libgromacs)
+        endif()
         if (WIN32)
-            # Avoid muParser assuming DLL export attributes should be added
-            target_compile_definitions(muparser_objlib PRIVATE MUPARSER_STATIC)
+            gmx_target_warning_suppression(muparser /wd4310 HAS_NO_MSVC_CAST_TRUNCATES_CONSTANT_VALUE)
         endif()
 
-        # Create an INTERFACE (ie. fake) library for muparser, that
-        # libgromacs can depend on. The generator expression for the
-        # target_sources expands to nothing when cmake builds the
-        # export for libgromacs, so that it understands that we don't
-        # install anything for this library - using plain source files
-        # would not convey the right information.
-        add_library(muparser INTERFACE)
-        target_sources(muparser INTERFACE $<TARGET_OBJECTS:muparser_objlib>)
-        target_include_directories(muparser SYSTEM INTERFACE $<BUILD_INTERFACE:${BUNDLED_MUPARSER_DIR}>)
-        # Add the muparser interface library to the libgromacs Export name, even though
-        # we will not be installing any content.
-        install(TARGETS muparser EXPORT libgromacs)
-
         set(HAVE_MUPARSER 1 CACHE INTERNAL "Is muparser found?")
     elseif(GMX_USE_MUPARSER STREQUAL "EXTERNAL")
         # Find an external muparser library.