Eawag-AppliedSystemAnalysis / Simstrat

Simstrat - 1D lake model
http://www.eawag.ch/en/department/surf/projects/simstrat/
GNU General Public License v3.0
17 stars 8 forks source link

Building Simstrat for macOS #35

Closed robertladwig closed 4 years ago

robertladwig commented 4 years ago

I've been following the steps in 'How to setup Simstrat building environment with Docker' to build a docker executable which should run for macOS (in my case Mojave). Running it inside the docker environment works fine, but the created executable can't be executed for macOS. The file type is

$ file simstrat 
simstrat: ELF 64-bit LSB pie executable x86-64, version 1 (SYSV), dynamically linked, with debug_info, not stripped

ELF (extensible and linkable format), whereas macOS would require Mach-OS for its binaries. Is there a workaround for the docker container to this?

Thanks!

d-vanzo commented 4 years ago

Hi @robertladwig and thanks for your issue.

I am not an expert of macOS environment, but what I can say is:

  1. the proposed docker solution is designed to generate an executable that runs inside docker (as you tested), so it is expectable that the executable is not working in your host system;
  2. sometimes linux executable works out-of-the-box on macOS, but it is not always true and... probably we are in that case;
  3. the docker image is based on alpine linux, so I guess it is even more unlikely that the executable works out of the docker;
  4. if, for some specific reasons, you really need a natively executable for macOS, one way is to create a cross-compiling toolchain from Linux to macOS, but this is not there and the FoBiS tool we are using does not include cross-compiling. Other way is to compile directly in macOS env.

I hope this helps

robertladwig commented 4 years ago

Thanks @d-vanzo. Points 1-3 make total sense to me, as the default approach would be to use the container for running Simstrat under macOS. I'm unsure if I know enough about cross-compiling toolchains to understand point 4. I've now tried to build it directly with FoBiS.py:

$ FoBiS.py build
Building dependency fobos into ../lib/csv_fortran with mode release-gnu
Builder options
  Directories
    Building directory: "build"
    Compiled-objects .o   directory: "build"
    Compiled-objects .mod directory: "build"
  Compiler options
    Vendor: "gnu"
    Compiler command: "gfortran"
    Module directory switch: "-J"
    Compiling flags: "-c -O2 -ffree-line-length-none -static-libgcc -static-libgfortran"
    Linking flags: ""
    Preprocessing flags: ""
    Coverage: False
    Profile: False
  Preprocessor used: None
  Preprocessor output directory: None
  Preprocessor extensions processed: []
Building src/csv_module.F90
Nothing to compile, all objects are up-to-date
Linking build/libcsv.a
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ranlib: file: build/libcsv.a(csv_kinds.o) has no symbols
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ranlib: file: build/libcsv.a(csv_parameters.o) has no symbols
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ranlib: file: build/libcsv.a(csv_kinds.o) has no symbols
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ranlib: file: build/libcsv.a(csv_parameters.o) has no symbols
Target src/csv_module.F90 has been successfully built
Building dependency fobos into ../lib/json_fortran with mode release-gnu
Attention: the file 'src/json_value_module.F90' depends on 'ifcore' that is unreachable
Builder options
  Directories
    Building directory: "build"
    Compiled-objects .o   directory: "build"
    Compiled-objects .mod directory: "build"
  Included paths: src 
  Compiler options
    Vendor: "gnu"
    Compiler command: "gfortran"
    Module directory switch: "-J"
    Compiling flags: "-c -O2 -ffree-line-length-none -static-libgcc -static-libgfortran"
    Linking flags: ""
    Preprocessing flags: ""
    Coverage: False
    Profile: False
  Preprocessor used: None
  Preprocessor output directory: None
  Preprocessor extensions processed: []
Building src/json_module.F90
Nothing to compile, all objects are up-to-date
Linking build/libjson.a
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ranlib: file: build/libjson.a(json_module.o) has no symbols
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ranlib: file: build/libjson.a(json_kinds.o) has no symbols
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ranlib: file: build/libjson.a(json_module.o) has no symbols
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ranlib: file: build/libjson.a(json_kinds.o) has no symbols
Target src/json_module.F90 has been successfully built
The following auxiliary paths have been added
Include files search paths (include):
- ../lib/csv_fortran/build
- ../lib/json_fortran/build
Libraries search paths (lib_dir):
Libraries paths:
- (libs) ../lib/csv_fortran/build/libcsv.a
- (libs) ../lib/json_fortran/build/libjson.a
Builder options
  Directories
    Building directory: "."
    Compiled-objects .o   directory: "obj"
    Compiled-objects .mod directory: "mod"
  Included paths: ../lib/csv_fortran/build ../lib/json_fortran/build 
  Linked libraries with full path: ../lib/csv_fortran/build/libcsv.a ../lib/json_fortran/build/libjson.a 
  Compiler options
    Vendor: "gnu"
    Compiler command: "gfortran"
    Module directory switch: "-J"
    Compiling flags: "-c -O2 -std=f2008 -ffree-line-length-none"
    Linking flags: "-static-libgcc -static-libgfortran -static"
    Preprocessing flags: ""
    Coverage: False
    Profile: False
  Preprocessor used: None
  Preprocessor output directory: None
  Preprocessor extensions processed: []
Building ../src/simstrat.f90
Compiling ../src/strat_kinds.f90 serially
Compiling ../src/utilities.f90 serially
Compiling ../src/strat_grid.f90 ../src/strat_consts.f90 serially
Compiling ../src/strat_simdata.f90 serially
Compiling ../src/strat_discretization.f90 ../src/strat_solver.f90 serially
Compiling ../src/strat_statevar.f90 serially
Compiling ../src/strat_forcing.f90 serially
Compiling ../src/strat_inputfile.f90 ../src/strat_outputfile.f90 ../src/strat_stability.f90 ../src/strat_windshear.f90 ../src/strat_temp.f90 ../src/strat_keps.f90 ../src/strat_turbulence.f90 ../src/strat_ice.f90 ../src/strat_transport.f90 ../src/strat_absorption.f90 ../src/strat_advection.f90 ../src/strat_lateral.f90 serially
Compiling ../src/simstrat.f90 serially
Linking ./simstrat
ld: library not found for -lcrt0.o
collect2: error: ld returned 1 exit status
ld: library not found for -lcrt0.o
collect2: error: ld returned 1 exit status

The building came really close, but at the end failed. This is probably related to the static linking that won't work on macOS X. I will try building it with the gcc compiler directly, but would it be possible to change the fobos file to build the code dynamically?

d-vanzo commented 4 years ago

Apparently is the same problem here

You can surely modify the fobos file and try, it is only a configuration file. Let us know if it works.

robertladwig commented 4 years ago

Heureka! Changing all static to dynamic did it:

[modes]
modes = release-gnu release-intel debug-gnu

[release-gnu]
compiler=Gnu
cflags=-c -O2 -std=f2008 -ffree-line-length-none
lflags=-dynamic-libgcc -dynamic-libgfortran -dynamic
build_dir=.
src=../src/
colors=False
quiet=False
target=simstrat.f90
dependon=../lib/csv_fortran/fobos:release-gnu((direct))
         ../lib/json_fortran/fobos:release-gnu((direct))
log=True

[release-intel]
compiler=intel
cflags=-c -O3
modsw=-module=
build_dir=.
src=../src/
colors=False
quiet=False
target=simstrat.f90
dependon=../lib/csv_fortran/fobos:release-intel((direct))
         ../lib/json_fortran/fobos:release-intel((direct))
log=True

[debug-gnu]
compiler=Gnu
cflags=-c -O2 -std=f2008 -ffree-line-length-none -g -ffpe-trap=overflow,zero -fno-unsafe-math-optimizations -frounding-math -fsignaling-nans
lflags=-dynamic-libgcc -dynamic-libgfortran -dynamic
build_dir=.
src=../src/
colors=False
quiet=False
target=simstrat.f90
dependon=../lib/csv_fortran/fobos:release-gnu((direct))
         ../lib/json_fortran/fobos:release-gnu((direct))
log=True

[rule-makedoc]
help = Generate FORD documentation (see /doc/developer folder)
rule = FoBiS.py rule -ford ../doc/developer/ford/ford_projectfile.md

And the created executable seems to work:

$ ./simstrat
 Simstrat version 2.2
 This software has been developed at eawag - Swiss Federal Institute of Aquatic Science and Technology

 [ERROR] File simstrat.par does not exist
d-vanzo commented 4 years ago

cool, so I'll introduce a further building mode (something like release-gnu-dynamic) and explain in the guidelines accordingly

Thank you for the feedback