sys-bio / roadrunner

libRoadRunner: A high-performance SBML simulator
http://libroadrunner.org/
Other
39 stars 24 forks source link

Expat Error while trying to install roadrunner from Source #1140

Closed Jan-Piotraschke closed 1 year ago

Jan-Piotraschke commented 1 year ago

Hi, I'm currently trying to install roadrunner from source on my M1 macOS to include it into my C++20 project.

I wrote a short Makefile script for this:

SHELL := /bin/bash

# Path to project directory
PROJECT_DIR := $(shell pwd)

.PHONY: build_libroadrunner
build_libroadrunner:
    @echo "Installing libRoadRunner from source..."
    if [ ! -d "roadrunner" ]; then \
        git clone https://github.com/sys-bio/roadrunner.git; \
    fi; \
    cd roadrunner; \
    mkdir -p build-release; \
    cd build-release; \
    LLVM_PATH=$$(brew --prefix llvm); \
    cmake -GNinja -DCMAKE_INSTALL_PREFIX="../install-release" \
        -DLLVM_INSTALL_PREFIX="$$LLVM_PATH" \
        -DRR_DEPENDENCIES_INSTALL_PREFIX="../../libroadrunner-deps/install-release" \
        -DCMAKE_BUILD_TYPE="Release" \
        -DCMAKE_CXX_STANDARD=17 ..; \
    ninja install

.PHONY: build_libroadrunner_deps
build_libroadrunner_deps:
    @echo "Installing libRoadRunner dependencies from source..."
    if [ ! -d "libroadrunner-deps" ]; then \
        git clone https://github.com/sys-bio/libroadrunner-deps.git --recurse-submodules; \
    fi; \
    cd libroadrunner-deps; \
    mkdir -p build; \
    cd build; \
    cmake -GNinja -DCMAKE_INSTALL_PREFIX="../install-release" \
        -DCMAKE_BUILD_TYPE="Release" \
        -DCMAKE_CXX_STANDARD=17 ..; \
    ninja install

But if I execute this script I run into an Expat Error:

expat::expat LOCATION_Release = /Users/jan-piotraschke/buildroadrunner/libroadrunner-deps/install-release/lib/libexpat.a
expat::expat MACOSX_PACKAGE_LOCATION = /Users/jan-piotraschke/buildroadrunner/libroadrunner-deps/install-release/lib/libexpat.a
expat::expat NAME = expat::expat
expat::expat SOURCE_DIR = /Users/jan-piotraschke/buildroadrunner/roadrunner
expat::expat SYSTEM = ON
expat::expat TYPE = STATIC_LIBRARY
expat::expat VS_DEPLOYMENT_LOCATION = /Users/jan-piotraschke/buildroadrunner/libroadrunner-deps/install-release/lib/libexpat.a
-- EXPAT_LIBRARY: /Users/jan-piotraschke/buildroadrunner/libroadrunner-deps/install-release/lib/libexpat.a
-- ZLIB_LIBRARY: /Users/jan-piotraschke/buildroadrunner/libroadrunner-deps/install-release/lib/libz.a
-- ZLIB_INCLUDE_DIR: /Users/jan-piotraschke/buildroadrunner/libroadrunner-deps/install-release/include/
CMake Error at /Users/jan-piotraschke/buildroadrunner/libroadrunner-deps/install-release/share/cmake/Modules/FindEXPAT.cmake:163 (message):
  Unable to compile a test executable against expat with

    EXPAT_INCLUDE_DIR = /Users/jan-piotraschke/buildroadrunner/libroadrunner-deps/install-release/include
    EXPAT_LIBRARY     = /Users/jan-piotraschke/buildroadrunner/libroadrunner-deps/install-release/lib/libexpat.a

Call Stack (most recent call first):
  /opt/homebrew/Cellar/cmake/3.26.4/share/cmake/Modules/CMakeFindDependencyMacro.cmake:76 (find_package)
  /Users/jan-piotraschke/buildroadrunner/libroadrunner-deps/install-release/lib/cmake/sbml-static-config.cmake:14 (find_dependency)
  CMakeLists.txt:460 (find_package)

-- Configuring incomplete, errors occurred!

Do you know how I can resolve this?

adelhpour commented 1 year ago

One of the things I noticed is that you are using a brew installed llvm. We highly recommend that you build the customized version of LLVM 13 from source and use it instead.

Jan-Piotraschke commented 1 year ago

Thanks for the tip! I build your customized version of LLVM 13 and adapted my Makefile script according to this:

.PHONY: build_libroadrunner
build_libroadrunner:
    @echo "Installing libRoadRunner from source..."
    if [ ! -d "roadrunner" ]; then \
        git clone https://github.com/sys-bio/roadrunner.git; \
    fi; \
    cd roadrunner; \
    mkdir -p build-release; \
    cd build-release; \
    cmake -GNinja -DCMAKE_INSTALL_PREFIX="../install-release" \
        -DLLVM_INSTALL_PREFIX="../../llvm-13.x/install-release" \
        -DRR_DEPENDENCIES_INSTALL_PREFIX="../../libroadrunner-deps/install-release" \
        -DCMAKE_BUILD_TYPE="Release" \
        -DCMAKE_CXX_STANDARD=17 ..; \
        ninja; \
    ninja install

.PHONY: build_llvm_13
build_llvm_13:
    @echo "Installing LLVM 13 from source..."; \
    if [ ! -d "llvm-13.x" ]; then \
        git clone https://github.com/sys-bio/llvm-13.x.git; \
    fi; \
    cd llvm-13.x; \
    mkdir -p build; \
    cd build; \
    cmake -GNinja -DCMAKE_INSTALL_PREFIX="../install-release" \
        -DCMAKE_BUILD_TYPE="Release" ../llvm; \
    ninja; \
    ninja install

But I am wondering if LLVM is the reason for this problem, because Expat is part of the libroadrunner-deps build and doesn't use LLVM.

In the end, the error message for building Roadrunner remains the same as provided above. What else can I do?

Jan

fbergmann commented 1 year ago

Just as explanation as to what is happening. The test to compile this file:

#include <expat.h>
#include <stdio.h>

int 
main(void)
{
    printf(XML_ExpatVersion());
    return 0;
}

fails when compiled and linked against the expat library. The test is needed to figure out, whether the expat requires the XML_STATIC compile flag defined or not.

As to why that compilation failed, I'm not sure. You should see the attempt in CMakeFiles/CMakeError.log.

Jan-Piotraschke commented 1 year ago

Awesome! I found the error:

  -
    kind: "try_compile-v1"
    backtrace:
      - "/opt/homebrew/Cellar/cmake/3.26.4/share/cmake/Modules/Internal/CheckSourceCompiles.cmake:101 (try_compile)"
      - "/opt/homebrew/Cellar/cmake/3.26.4/share/cmake/Modules/CheckCSourceCompiles.cmake:76 (cmake_check_source_compiles)"
      - "/Users/jan-piotraschke/buildroadrunner/libroadrunner-deps/install-release/share/cmake/Modules/FindEXPAT.cmake:150 (CHECK_C_SOURCE_COMPILES)"
      - "/opt/homebrew/Cellar/cmake/3.26.4/share/cmake/Modules/CMakeFindDependencyMacro.cmake:76 (find_package)"
      - "/Users/jan-piotraschke/buildroadrunner/libroadrunner-deps/install-release/lib/cmake/sbml-static-config.cmake:14 (find_dependency)"
      - "CMakeLists.txt:460 (find_package)"
    checks:
      - "Performing Test EXPAT_EXPAT_TEST"
    directories:
      source: "/Users/jan-piotraschke/buildroadrunner/roadrunner/build-release/CMakeFiles/CMakeScratch/TryCompile-gaWdKr"
      binary: "/Users/jan-piotraschke/buildroadrunner/roadrunner/build-release/CMakeFiles/CMakeScratch/TryCompile-gaWdKr"
    cmakeVariables:
      CMAKE_C_FLAGS: ""
      CMAKE_C_FLAGS_DEBUG: "-g"
      CMAKE_EXE_LINKER_FLAGS: ""
      CMAKE_MODULE_PATH: "/Users/jan-piotraschke/buildroadrunner/roadrunner/cmake;/Users/jan-piotraschke/buildroadrunner/libroadrunner-deps/install-release/lib/cmake;/Users/jan-piotraschke/buildroadrunner/libroadrunner-deps/install-release/lib64/cmake;/Users/jan-piotraschke/buildroadrunner/libroadrunner-deps/install-release/cmake;/Users/jan-piotraschke/buildroadrunner/libroadrunner-deps/install-release/lib/cmake/Poco;/Users/jan-piotraschke/buildroadrunner/libroadrunner-deps/install-release/share/cmake/Modules"
      CMAKE_OSX_ARCHITECTURES: "x86_64"
      CMAKE_OSX_DEPLOYMENT_TARGET: ""
      CMAKE_OSX_SYSROOT: "/Library/Developer/CommandLineTools/SDKs/MacOSX13.3.sdk"
      CMAKE_POSITION_INDEPENDENT_CODE: "ON"
    buildResult:
      variable: "EXPAT_EXPAT_TEST"
      cached: true
      stdout: |
        Change Dir: /Users/jan-piotraschke/buildroadrunner/roadrunner/build-release/CMakeFiles/CMakeScratch/TryCompile-gaWdKr

        Run Build Command(s):/opt/homebrew/Cellar/cmake/3.26.4/bin/cmake -E env VERBOSE=1 /usr/bin/make -f Makefile cmTC_03476/fast && /Library/Developer/CommandLineTools/usr/bin/make  -f CMakeFiles/cmTC_03476.dir/build.make CMakeFiles/cmTC_03476.dir/build
        Building C object CMakeFiles/cmTC_03476.dir/src.c.o
        /Library/Developer/CommandLineTools/usr/bin/cc -DEXPAT_EXPAT_TEST -I/Users/jan-piotraschke/buildroadrunner/libroadrunner-deps/install-release/include -arch x86_64 -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX13.3.sdk -fPIE -MD -MT CMakeFiles/cmTC_03476.dir/src.c.o -MF CMakeFiles/cmTC_03476.dir/src.c.o.d -o CMakeFiles/cmTC_03476.dir/src.c.o -c /Users/jan-piotraschke/buildroadrunner/roadrunner/build-release/CMakeFiles/CMakeScratch/TryCompile-gaWdKr/src.c
        /Users/jan-piotraschke/buildroadrunner/roadrunner/build-release/CMakeFiles/CMakeScratch/TryCompile-gaWdKr/src.c:8:12: warning: format string is not a string literal (potentially insecure) [-Wformat-security]
            printf(XML_ExpatVersion());
                   ^~~~~~~~~~~~~~~~~~
        /Users/jan-piotraschke/buildroadrunner/roadrunner/build-release/CMakeFiles/CMakeScratch/TryCompile-gaWdKr/src.c:8:12: note: treat the string as an argument to avoid this
            printf(XML_ExpatVersion());
                   ^
                   "%s", 
        1 warning generated.
        Linking C executable cmTC_03476
        /opt/homebrew/Cellar/cmake/3.26.4/bin/cmake -E cmake_link_script CMakeFiles/cmTC_03476.dir/link.txt --verbose=1
        /Library/Developer/CommandLineTools/usr/bin/cc  -arch x86_64 -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX13.3.sdk -Wl,-search_paths_first -Wl,-headerpad_max_install_names CMakeFiles/cmTC_03476.dir/src.c.o -o cmTC_03476  /Users/jan-piotraschke/buildroadrunner/libroadrunner-deps/install-release/lib/libexpat.a 
        ld: warning: ignoring file /Users/jan-piotraschke/buildroadrunner/libroadrunner-deps/install-release/lib/libexpat.a, building for macOS-x86_64 but attempting to link with file built for macOS-arm64
        Undefined symbols for architecture x86_64:
          "_XML_ExpatVersion", referenced from:
              _main in src.c.o
        ld: symbol(s) not found for architecture x86_64
        clang: error: linker command failed with exit code 1 (use -v to see invocation)
        make[2]: *** [cmTC_03476] Error 1
        make[1]: *** [cmTC_03476/fast] Error 2

      exitCode: 2

Somehow, sometimes my macOS compiled builds for the x86_64 and sometimes for the arm64 architecture.

Setting the architecture in the Makefile solved my problem and Roadrunner compiled successfully.

ARCHITECTURE := arm64

.PHONY: build_libroadrunner
build_libroadrunner:
    @echo "Installing libRoadRunner from source..."
    if [ ! -d "roadrunner" ]; then \
        git clone https://github.com/sys-bio/roadrunner.git; \
    fi; \
    cd roadrunner; \
    mkdir -p build-release; \
    cd build-release; \
    cmake -GNinja -DCMAKE_INSTALL_PREFIX="../install-release" \
        -DLLVM_INSTALL_PREFIX="../../llvm-13.x/install-release" \
        -DRR_DEPENDENCIES_INSTALL_PREFIX="../../libroadrunner-deps/install-release" \
        -DCMAKE_BUILD_TYPE="Release" \
        -DCMAKE_CXX_STANDARD=17 \
        -DCMAKE_OSX_ARCHITECTURES=$(ARCHITECTURE) ..; \
    ninja; \
    ninja install

Thank you both for your quick help!

Jan