Commit 8079c98c authored by Artur Grunau's avatar Artur Grunau
Browse files

Fix out-of-tree and Visual Studio builds

Out-of-tree builds were broken due to Lua and Swig header files not
being correcly included. The state of Visual Studio builds was even
worse: they failed because Lua modules, scripts and libraries were
misplaced.

The issues mentioned above have been fixed by making the build system
properly deal with out-of-tree builds and multi-config generators.

References #1
parent 31df41df
......@@ -7,10 +7,10 @@
IF(WIN32)
SET(LUA_DIR "${CampvisHome}/ext/lua" CACHE PATH "If Lua is not found, set this path")
SET(LUA_INCLUDE_DIR "${LUA_DIR}/src" ${LUA_DIR})
SET(LUA_INCLUDE_DIR "${LUA_DIR}/src" "${CMAKE_BINARY_DIR}/ext/lua")
IF(LUA_INCLUDE_DIR)
SET(LUA_LIBRARY "${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/lua.lib")
SET(LUA_LIBRARY liblua)
SET(LUA_FOUND TRUE)
ELSE()
SET(LUA_FOUND FALSE)
......
......@@ -38,7 +38,7 @@ SWIG_LINK_LIBRARIES(vis campvis-core campvis-modules tgt ${CampvisGlobalExternal
ADD_EXECUTABLE(campvis-scripting ${CampvisScriptingSources} ${CampvisScriptingHeaders})
ADD_DEFINITIONS(${CampvisGlobalDefinitions} ${CampvisScriptingDefinitions})
INCLUDE_DIRECTORIES(${CampvisGlobalIncludeDirs})
INCLUDE_DIRECTORIES(${CampvisGlobalIncludeDirs} ${CMAKE_CURRENT_BINARY_DIR})
TARGET_LINK_LIBRARIES(campvis-scripting campvis-core campvis-modules tgt ${CampvisGlobalExternalLibs})
# Generate header with SWIG run-time functions
......@@ -48,10 +48,11 @@ ADD_CUSTOM_COMMAND(
COMMAND ${SWIG_EXECUTABLE} -c++ -lua -external-runtime "swigluarun.h"
)
# Copy Lua test scripts into the target directory
ADD_CUSTOM_COMMAND(
TARGET campvis-scripting
POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy "script.lua" ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}
)
SET_SOURCE_FILES_PROPERTIES(testapp.cpp PROPERTIES OBJECT_DEPENDS "scripting/script.lua")
# If a multi-config generator is used (e.g. Visual Studio or XCode), CAMPVis Lua modules will be
# placed in a sub-directory that won't be picked up by Lua automatically. Let the code know via
# a #define what that sub-directory is so that it can instruct Lua to search it.
FOREACH(CONFIG_TYPE ${CMAKE_CONFIGURATION_TYPES})
STRING(TOUPPER ${CONFIG_TYPE} CONFIG_TYPE_UPPER)
SET_DIRECTORY_PROPERTIES(PROPERTIES COMPILE_DEFINITIONS_${CONFIG_TYPE_UPPER}
"CAMPVIS_LUA_MODS_PATH=${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/${CONFIG_TYPE}/?${CMAKE_SHARED_MODULE_SUFFIX}")
ENDFOREACH(CONFIG_TYPE CMAKE_CONFIGURATION_TYPES)
......@@ -10,16 +10,9 @@ extern "C" {
namespace campvis {
void callLuaFunc(lua_State* _luaState, int nargs, int nresults) {
void LuaPipeline::callLuaFunc(lua_State* _luaState, int nargs, int nresults) {
if (lua_pcall(_luaState, nargs, nresults, 0) != LUA_OK) {
const char* errorMsg = lua_tostring(_luaState, -1);
if (errorMsg == nullptr)
std::cout << "(error object is not a string)" << std::endl;
else
std::cout << errorMsg << std::endl;
lua_pop(_luaState, 1);
this->logLuaError();
}
}
......@@ -31,7 +24,31 @@ namespace campvis {
// load the libs
luaL_openlibs(_luaState);
luaL_dostring(_luaState, "require(\"campvis\")");
/*
* Defined if CAMPVis Lua modules are placed in a location that won't be picked up by Lua
* automatically
*/
#ifdef CAMPVIS_LUA_MODS_PATH
#define STRINGIFY(x) #x
#define TOSTRING(x) STRINGIFY(x)
/* Let Lua know where CAMPVis modules are located */
if (luaL_dostring(_luaState, "package.cpath = package.cpath .. ';" TOSTRING(CAMPVIS_LUA_MODS_PATH) "'")) {
this->logLuaError();
return;
}
#undef TOSTRING
#undef STRINGIFY
#endif // CAMPVIS_LUA_MODS_PATH
if (luaL_dostring(_luaState, "require(\"campvis\")")) {
this->logLuaError();
return;
}
swig_type_info* autoEvaluationPipelineType = SWIG_TypeQuery(_luaState, "campvis::AutoEvaluationPipeline *");
......@@ -44,14 +61,8 @@ namespace campvis {
// run a Lua script here; true is returned if there were errors
if (luaL_dofile(_luaState, scriptPath.c_str())) {
const char* msg = lua_tostring(_luaState, -1);
if (msg == nullptr)
printf("(error object is not a string)");
else
printf("%s", msg);
lua_pop(_luaState, 1);
this->logLuaError();
return;
}
lua_getglobal(_luaState, "pipeline");
......@@ -72,6 +83,17 @@ namespace campvis {
lua_close(_luaState);
}
void LuaPipeline::logLuaError() {
const char* errorMsg = lua_tostring(_luaState, -1);
if (errorMsg == nullptr)
std::cerr << "(error object is not a string)" << std::endl;
else
std::cerr << errorMsg << std::endl;
lua_pop(_luaState, 1);
}
void LuaPipeline::init() {
AutoEvaluationPipeline::init();
......
......@@ -35,6 +35,16 @@ namespace campvis {
virtual void deinit();
protected:
/**
* Log the most recent Lua error to the console.
*/
void logLuaError();
/**
* Call the Lua function that's at the top of the stack.
*/
void callLuaFunc(lua_State* _luaState, int nargs, int nresults);
const std::string _scriptPath; ///< path to the Lua script defining the pipeline
lua_State* _luaState; ///< Lua state used to evaluate the pipeline
};
......
......@@ -9,7 +9,7 @@ int main()
{
PipelineFactory& pipelineFactory = PipelineFactory::getRef();
pipelineFactory.registerPipeline<LuaPipeline>([] (DataContainer* dc) -> AbstractPipeline* {
return new LuaPipeline("script.lua", dc);
return new LuaPipeline(CAMPVIS_SOURCE_DIR "/scripting/script.lua", dc);
});
DataContainer* dc = new DataContainer("Test Data Container");
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment