admb-project / admb

AD Model Builder
http://admb-project.org
Other
64 stars 19 forks source link

Feature request allow CXX=g++-{version number} in linux #270

Closed yukio-takeuchi closed 2 years ago

yukio-takeuchi commented 2 years ago

Hi, some of linux dists (e.g. Ubuntu) allow user to install versions of gcc(g++) other than their default version. Although they allow to change default version, if ADMB easily allow user to choose e.g. CXX=g++-8, ADMB can have more flexibility. In aprticular, it benefits for linuxbrew (homebrew in linux) as their version of gcc and g++ commands are gcc-12 and g++-12 respectively. Is it possible to allow user to compile admb with commad like

make g++-12  or make g++-8
johnoel commented 2 years ago

Sorry @yukio-takeuchi looks like we both worked on the same issue. Anyhow, in dev-13.1 has the changes for this issue. It seems to work with the command below.

% make g++-12

Another way to build with Homebrew's compiler naming convention is

admb-main % make CXX=g++-12 CC=gcc-12 --directory=src all
admb-main % make CXX=g++-12 CC=gcc-12 --directory=contrib all

where version 12 can be changed for other numbers.

yukio-takeuchi commented 2 years ago

Hi @johnoel

The changes made in dev-13.1 branch looks better than my pull request. This also allows to use g++ from homebrew on max as well

Thanks @johnoel

johnoel commented 2 years ago

Thanks @yukio-takeuchi for reviewing the changes. Is it okay to close?

yukio-takeuchi commented 2 years ago

Hi @johnoel

Sorry for delayed response.

It seems, there remain a linker error when

make g++-12-debug

as

mkdir -p ../build/admb/lib
rm -vf ../build/admb/lib/libadmb-x86_64-macos-g++12-shared-debug.a
rm -vf ../build/admb/lib/libadmbo-x86_64-macos-g++12-shared-debug.a
ar -rs ../build/admb/lib/libadmb-x86_64-macos-g++12-shared-debug.a ../build/objects-x86_64-macos-g++12-shared-debug/saflp-linad99-[f]*.obj
ar: creating archive ../build/admb/lib/libadmb-x86_64-macos-g++12-shared-debug.a
ar -rs ../build/admb/lib/libadmb-x86_64-macos-g++12-shared-debug.a ../build/objects-x86_64-macos-g++12-shared-debug/saflp-linad99-[^f]*.obj
ar -rs ../build/admb/lib/libadmb-x86_64-macos-g++12-shared-debug.a ../build/objects-x86_64-macos-g++12-shared-debug/saflp-df1b2-separable-*.obj
ar -rs ../build/admb/lib/libadmb-x86_64-macos-g++12-shared-debug.a ../build/objects-x86_64-macos-g++12-shared-debug/saflp-nh99-*.obj
ar -rs ../build/admb/lib/libadmb-x86_64-macos-g++12-shared-debug.a ../build/objects-x86_64-macos-g++12-shared-debug/saflp-tools99-*.obj
ar -rs ../build/admb/lib/libadmb-x86_64-macos-g++12-shared-debug.a ../build/objects-x86_64-macos-g++12-shared-debug/saflp-sparse-*.obj
ar -rs ../build/admb/lib/libadmbo-x86_64-macos-g++12-shared-debug.a ../build/objects-x86_64-macos-g++12-shared-debug/optlp-linad99-[f]*.obj
ar: creating archive ../build/admb/lib/libadmbo-x86_64-macos-g++12-shared-debug.a
ar -rs ../build/admb/lib/libadmbo-x86_64-macos-g++12-shared-debug.a ../build/objects-x86_64-macos-g++12-shared-debug/optlp-linad99-[^f]*.obj
ar -rs ../build/admb/lib/libadmbo-x86_64-macos-g++12-shared-debug.a ../build/objects-x86_64-macos-g++12-shared-debug/optlp-df1b2-separable-*.obj
ar -rs ../build/admb/lib/libadmbo-x86_64-macos-g++12-shared-debug.a ../build/objects-x86_64-macos-g++12-shared-debug/optlp-nh99-*.obj
ar -rs ../build/admb/lib/libadmbo-x86_64-macos-g++12-shared-debug.a ../build/objects-x86_64-macos-g++12-shared-debug/optlp-tools99-*.obj
ar -rs ../build/admb/lib/libadmbo-x86_64-macos-g++12-shared-debug.a ../build/objects-x86_64-macos-g++12-shared-debug/optlp-sparse-*.obj
/Applications/Xcode.app/Contents/Developer/usr/bin/make bins
mkdir -p ../build/admb/bin
cp df1b2-separable/seddf1b* df1b2-separable/sedf1b2* ../build/admb/bin
echo "ADMB_CFG_CXXFLAGS=">../build/admb/bin/admb-cfg-x86_64-macos-g++12.sh
echo "ADMB_CFG_LDFLAGS=">>../build/admb/bin/admb-cfg-x86_64-macos-g++12.sh
cp ../scripts/admb/admb ../build/admb/bin
cp ../scripts/admb/adlink ../build/admb/bin
cp ../scripts/admb/adcomp ../build/admb/bin
ln -sf bin/admb ../build/admb
#cp ../scripts/admb/root-admb ../build/admb/admb
ln -sf build/admb/bin/admb ..
#cp ../scripts/admb/root-admb ../admb
mkdir -p ../build/tpl2rem
/Applications/Xcode.app/Contents/Developer/usr/bin/make --directory=df1b2-separable CC=gcc-12 DIST=../../build/admb CMDSHELL= bins
make[5]: Nothing to be done for `bins'.
mkdir -p ../build/tpl2cpp
/Applications/Xcode.app/Contents/Developer/usr/bin/make --directory=nh99 CC=gcc-12 DIST=../../build/admb CMDSHELL= bins
make[5]: Nothing to be done for `bins'.
g++-12 -shared  -o ../build/admb/lib/libadmb-x86_64-macos-g++12.so -Wl,--whole-archive ../build/admb/lib/libadmb-x86_64-macos-g++12-shared-debug.a -Wl,--no-whole-archive
ld: unknown option: --whole-archive
collect2: error: ld returned 1 exit status
make[3]: *** [shared] Error 1
make[2]: *** [g++-12-shared] Error 2
make[1]: *** [g++-12-all] Error 2
make: *** [g++-12-debug] Error 2

This error is probably related to the fact that g++ from homebrew uses the linker from Apple (see here .

In src/GNUmakefile and contrib/GNUmakrfile, if debug is not Yes, it seems, this problem is resolved but if debug is Yes, the problem may remain.

Thanks

yukio-takeuchi commented 2 years ago

Hi @johnoel

In addition, when I tried compiling "simple" in examples/admb/simple by

export CXX=g++-12;/usr/local/opt/admb-gcc/bin/admb -p simple

to make a "portable" executable with admb compiled with g++-12 without "debug=yes", it failed to link as

➜ export CXX=g++-12;/usr/local/opt/admb-gcc/bin/admb -p simple
*** Parse: simple.tpl
tpl2cpp simple || tpl2rem simple

*** Compile: simple.cpp
g++-12 -c -std=c++17 -O3 -D_USE_MATH_DEFINES -DUSE_ADMB_CONTRIBS -I. -I"/usr/local/opt/admb-gcc/include" -I"/usr/local/opt/admb-gcc/include/contrib" -osimple.obj simple.cpp

*** Linking: simple.obj
g++-12 --static -std=c++17 -O3 -osimple simple.obj "/usr/local/opt/admb-gcc/lib/libadmb-contrib-x86_64-macos-g++12.a"

ld: library not found for -lcrt0.o
collect2: error: ld returned 1 exit status
Error: Could not build simple

This is simply because gcc (and g++) from homebrew on Mac uses linker from Apple so that "--static" is not accepted. Simple solution may be to change lines from 344 in script/admb/admb

if [[ "$static" == "-static" || "$CXX" == "adcomp-x86_64-w64-mingw32" ]]; then
   LDFLAGS="--static $LDFLAGS"
fi

to

if [[ "$static" == "-static" || "$CXX" == "adcomp-x86_64-w64-mingw32" ]]; then
    if [ "`uname`" == "Darwin" ]  && [[ "$CXX" =~ "g++-" ]] && [[ "`$CXX --version`" =~ "Homebrew" ]]; then
      #LDFLAGS="-lm -lstdc++ -static-libgcc -static-libstdc++ $LDFLAGS"
      LDFLAGS=" -static-libgcc -static-libstdc++ $LDFLAGS"
    else
      LDFLAGS="--static $LDFLAGS"
    fi
fi

Linker still produce warning but it allows to produce executable.

Thanks

johnoel commented 2 years ago

Okay @yukio-takeuchi, try updated fixes.

Please rebuild with Homebrew.

admb-main % PATH=/opt/homebrew/bin:$PATH CXX=g++-12 make DEBUG=yes -j 8 

Then build simple

simple % PATH=/opt/homebrew/bin:$PATH CXX=g++-12 ../../../admb -p simple
*** Parse: simple.tpl
tpl2cpp simple || tpl2rem simple

*** Compile: simple.cpp
g++-12 -c -std=c++17 -O3 -D_USE_MATH_DEFINES -DUSE_ADMB_CONTRIBS -I. -I"/Users/johnoel/admb-main/build/admb/include" -I"/Users/johnoel/admb-main/build/admb/include/contrib" -osimple.obj simple.cpp

*** Linking: simple.obj 
g++-12 -Bstatic -std=c++17 -O3 -osimple simple.obj "/Users/johnoel/admb-main/build/admb/lib/libadmb-contrib-arm64-macos-g++12-debug.a"

Successfully built executable.
yukio-takeuchi commented 2 years ago

Thanks @johnoel,

As I use my Mac at home to build and will go to office right now,I will try building this evening and will report back

Cheers,

yukio-takeuchi commented 2 years ago

Hi @johnoel

I was also able to finish compiling simple now as

➜ export CXX=g++-12;/usr/local/opt/admb-gcc/bin/admb -p -g simple
*** Parse: simple.tpl
tpl2cpp -debug simple || tpl2rem -debug simple

*** Compile: simple.cpp
g++-12 -c -std=c++17 -g -DDEBUG -D_USE_MATH_DEFINES -DUSE_ADMB_CONTRIBS -I. -I"/usr/local/opt/admb-gcc/include" -I"/usr/local/opt/admb-gcc/include/contrib" -osimple.obj simple.cpp

*** Linking: simple.obj
g++-12 -Bstatic -std=c++17 -g -DDEBUG -osimple simple.obj "/usr/local/opt/admb-gcc/lib/libadmb-contrib-x86_64-macos-g++12.a"

Successfully built executable.

examples/admb/simple on  dev-13.1 [!?⇣]
➜ export CXX=g++-12;/usr/local/opt/admb-gcc/bin/admb -p  simple
*** Parse: simple.tpl
tpl2cpp simple || tpl2rem simple

*** Compile: simple.cpp
g++-12 -c -std=c++17 -O3 -D_USE_MATH_DEFINES -DUSE_ADMB_CONTRIBS -I. -I"/usr/local/opt/admb-gcc/include" -I"/usr/local/opt/admb-gcc/include/contrib" -osimple.obj simple.cpp

*** Linking: simple.obj
g++-12 -Bstatic -std=c++17 -O3 -osimple simple.obj "/usr/local/opt/admb-gcc/lib/libadmb-contrib-x86_64-macos-g++12.a"

Successfully built executable.

Thank you so much

johnoel commented 2 years ago

Okay, if all good, can you close?