smanders / externpro

build external projects with cmake
MIT License
13 stars 12 forks source link

compile lua as C++ #289

Closed smanders closed 2 years ago

smanders commented 3 years ago

From: Brian Simpson Sent: Tuesday, November 17, 2020 11:50 PM To: Steven Woolsey; Scott M Anderson Subject: compiling lua as C++?

Guys, Now that we’re calling some pretty complex c++ functions from our lua scripts, I’m encountering some throws. They are of a nature that we can’t have great confidence that they would be caught in testing.

Therefore, I’m convinced that we need to be able to catch and handle them, reporting them as robust error messages.

However… When they happen, the c code of lua simply exits the application. That is because it is compiled as C, therefore it is unable to handle C++ exceptions.

It appears that Lua has actually been designed to be compiled either as C or as C++ (http://www.lua.org/manual/5.4/readme.html#install) and, furthermore, that this is the principal reason for doing so (https://stackoverflow.com/a/8403962/5290879 -- and the subsequent comment at that link actually argues that, in the situation I think we are in, there’s no reason not to compile as C++).

the question is: Is there any reason we really need lua compiled as C? Or is it possible that we could provide 2 builds, one as C and one as C++?

(I’m going to be a bit embarrassed if you tell me that it is already C++, so then I’ll just get with Steve and we’ll see what I’m doing wrong.)

Thanks! --Brian

p.s. I don’t know absolutely, but it seems that we simply change src/Makefile, changing the line, “CC= gcc -std=gnu99", to something like “CC= g++ -std=c++11".

smanders commented 3 years ago

I can confirm that our current build of lua, which is built via cmake (not the Makefiles in the repo), builds with the C compiler, not the C++ compiler

-- The C compiler identification is GNU 7.3.1
-- Check for working C compiler: /opt/rh/devtoolset-7/root/usr/bin/cc
-- Check for working C compiler: /opt/rh/devtoolset-7/root/usr/bin/cc - works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Performing Test has_c_fPIC
-- Performing Test has_c_fPIC - Success
-- Performing Test has_c_msse3
-- Performing Test has_c_msse3 - Success
-- Performing Test has_c_StrongSP
-- Performing Test has_c_StrongSP - Success
DIST_NAME: lua
DIST_VERSION: 5.2.3
DIST_LICENSE: MIT/X11
DIST_AUTHOR: Roberto Ierusalimschy, Waldemar Celes, Luiz Henrique de Figueiredo
DIST_MAINTAINER: Peter Drahoš
DIST_URL: http://www.lua.org
DIST_DESC: Lua is a powerful, fast, light-weight, embeddable scripting language.
DIST_DEPENDS: 
-- Found PkgConfig: /usr/bin/pkg-config (found version "0.27.1") 
-- Could NOT find Readline (missing: READLINE_LIBRARY READLINE_INCLUDE_DIR) 
-- Could NOT find Curses (missing: CURSES_LIBRARY CURSES_INCLUDE_PATH) 
-- Configuring done
-- Generating done
-- Build files have been written to: /srcdir/externpro/_bldlua/xpbase/Build/lua_Release
[ 62%] Performing build step for 'lua_Release'
Scanning dependencies of target liblua
[  1%] Building C object CMakeFiles/liblua.dir/src/lapi.c.o
[  2%] Building C object CMakeFiles/liblua.dir/src/lcode.c.o
[  4%] Building C object CMakeFiles/liblua.dir/src/lctype.c.o
[  5%] Building C object CMakeFiles/liblua.dir/src/ldebug.c.o

notice that only the C compiler is detected at the beginning of the cmake run, and "Building C object" as the source is compiled

smanders commented 3 years ago

replacing C with CXX exposes cmake errors in externpro, and adding CXX isn't enough

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 12f1aae..6877fbd 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -4,7 +4,7 @@
 # For details see the COPYRIGHT file distributed with LuaDist.
 # Please note that the package source code is licensed under its own license.

-project ( lua C )
+project ( lua C CXX )
 cmake_minimum_required ( VERSION 2.8 )
 include ( flags OPTIONAL )
 include ( cmake/dist.cmake )
-- The C compiler identification is GNU 7.3.1
-- The CXX compiler identification is GNU 7.3.1
-- Check for working C compiler: /opt/rh/devtoolset-7/root/usr/bin/cc
-- Check for working C compiler: /opt/rh/devtoolset-7/root/usr/bin/cc - works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /opt/rh/devtoolset-7/root/usr/bin/c++
-- Check for working CXX compiler: /opt/rh/devtoolset-7/root/usr/bin/c++ - works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Performing Test has_c_fPIC
-- Performing Test has_c_fPIC - Success
-- Performing Test has_c_msse3
-- Performing Test has_c_msse3 - Success
-- Performing Test has_c_StrongSP
-- Performing Test has_c_StrongSP - Success
-- Performing Test has_cxx_fPIC
-- Performing Test has_cxx_fPIC - Success
-- Performing Test has_cxx_msse3
-- Performing Test has_cxx_msse3 - Success
-- Performing Test has_cxx_StrongSP
-- Performing Test has_cxx_StrongSP - Success
DIST_NAME: lua
DIST_VERSION: 5.2.3
DIST_LICENSE: MIT/X11
DIST_AUTHOR: Roberto Ierusalimschy, Waldemar Celes, Luiz Henrique de Figueiredo
DIST_MAINTAINER: Peter Drahoš
DIST_URL: http://www.lua.org
DIST_DESC: Lua is a powerful, fast, light-weight, embeddable scripting language.
DIST_DEPENDS: 
-- Found PkgConfig: /usr/bin/pkg-config (found version "0.27.1") 
-- Could NOT find Readline (missing: READLINE_LIBRARY READLINE_INCLUDE_DIR) 
-- Could NOT find Curses (missing: CURSES_LIBRARY CURSES_INCLUDE_PATH) 
-- Configuring done
-- Generating done
-- Build files have been written to: /srcdir/externpro/_bldlua/xpbase/Build/lua_Release
[ 62%] Performing build step for 'lua_Release'
Scanning dependencies of target liblua
[  1%] Building C object CMakeFiles/liblua.dir/src/lapi.c.o
[  2%] Building C object CMakeFiles/liblua.dir/src/lcode.c.o
[  4%] Building C object CMakeFiles/liblua.dir/src/lctype.c.o
[  5%] Building C object CMakeFiles/liblua.dir/src/ldebug.c.o

the C++ compiler is also detected at the beginning of the cmake run, but still "Building C object" as the source is compiled

smanders commented 3 years ago

however, if I add a make rules override file (and also put the cmake_minimum_required() call first, as it's supposed to be)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 12f1aae..503b048 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -4,8 +4,9 @@
 # For details see the COPYRIGHT file distributed with LuaDist.
 # Please note that the package source code is licensed under its own license.

-project ( lua C )
 cmake_minimum_required ( VERSION 2.8 )
+set ( CMAKE_USER_MAKE_RULES_OVERRIDE cmake/makeRulesOverwrite.cmake )
+project ( lua C CXX )
 include ( flags OPTIONAL )
 include ( cmake/dist.cmake )

diff --git a/cmake/makeRulesOverwrite.cmake b/cmake/makeRulesOverwrite.cmake
new file mode 100644
index 0000000..e1d6095
--- /dev/null
+++ b/cmake/makeRulesOverwrite.cmake
@@ -0,0 +1 @@
+list(APPEND CMAKE_CXX_SOURCE_FILE_EXTENSIONS c)
-- The C compiler identification is GNU 7.3.1
-- The CXX compiler identification is GNU 7.3.1
-- Check for working C compiler: /opt/rh/devtoolset-7/root/usr/bin/cc
-- Check for working C compiler: /opt/rh/devtoolset-7/root/usr/bin/cc - works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /opt/rh/devtoolset-7/root/usr/bin/c++
-- Check for working CXX compiler: /opt/rh/devtoolset-7/root/usr/bin/c++ - works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Performing Test has_c_fPIC
-- Performing Test has_c_fPIC - Success
-- Performing Test has_c_msse3
-- Performing Test has_c_msse3 - Success
-- Performing Test has_c_StrongSP
-- Performing Test has_c_StrongSP - Success
-- Performing Test has_cxx_fPIC
-- Performing Test has_cxx_fPIC - Success
-- Performing Test has_cxx_msse3
-- Performing Test has_cxx_msse3 - Success
-- Performing Test has_cxx_StrongSP
-- Performing Test has_cxx_StrongSP - Success
DIST_NAME: lua
DIST_VERSION: 5.2.3
DIST_LICENSE: MIT/X11
DIST_AUTHOR: Roberto Ierusalimschy, Waldemar Celes, Luiz Henrique de Figueiredo
DIST_MAINTAINER: Peter Drahoš
DIST_URL: http://www.lua.org
DIST_DESC: Lua is a powerful, fast, light-weight, embeddable scripting language.
DIST_DEPENDS: 
-- Found PkgConfig: /usr/bin/pkg-config (found version "0.27.1") 
-- Could NOT find Readline (missing: READLINE_LIBRARY READLINE_INCLUDE_DIR) 
-- Could NOT find Curses (missing: CURSES_LIBRARY CURSES_INCLUDE_PATH) 
-- Configuring done
-- Generating done
-- Build files have been written to: /srcdir/externpro/_bldlua/xpbase/Build/lua_Release
[ 62%] Performing build step for 'lua_Release'
Scanning dependencies of target liblua
[  1%] Building CXX object CMakeFiles/liblua.dir/src/lapi.c.o
[  2%] Building CXX object CMakeFiles/liblua.dir/src/lcode.c.o
[  4%] Building CXX object CMakeFiles/liblua.dir/src/lctype.c.o
[  5%] Building CXX object CMakeFiles/liblua.dir/src/ldebug.c.o

notice: "Building CXX object" as the source is compiled

I've tested that this also builds successfully on Windows, but I don't have any visual indication on Windows that it's using the C++ compiler and not the C compiler

smanders commented 3 years ago

moved to "future", since it appears to be less of an issue to the developers using lua

smanders commented 2 years ago

closing issue, for now... if we ever decide to compile lua with C++ compiler instead of C compiler, this issue has the information on how to do so