Jump to content

CMake file for both Linux and Windows


Era
 Share

Recommended Posts

With VS2017 supporting CMake files, and my none love for Code::Blocks I have written a CMake file that works on both Linux and Windows.(I have not tested the $ENV{HOME} yet, I had that hard coded).

 

There is one issue I know of where VS2017 does not seem to obey the CMAKE_RUNTIME_OUTPUT_DIRECTORY line and puts the binaries in a folder called Debug or Release in side your project directoy. You can safely move them out to the top level.

 

cmake_minimum_required (VERSION 2.8.11)
project (Demo)

if (CMAKE_SYSTEM_NAME STREQUAL "Linux")
set(LEADWERKS_PATH "$ENV{HOME}/.steam/steam/steamapps/common/Leadwerks")
else (CMAKE_SYSTEM_NAME STREQUAL "Linux")
set(LEADWERKS_PATH "c:/Program Files (x86)/Steam/steamapps/common/Leadwerks")
endif (CMAKE_SYSTEM_NAME STREQUAL "Linux")

set(CMAKE_CONFIGURATION_TYPES "Release;Debug" CACHE STRING "" FORCE)

set(SOURCE
   Source/App.cpp
   Source/main.cpp
)

set(HEADERS
   Source/App.h
)

FILE(GLOB_RECURSE LUAFiles "Scripts/*.lua")
add_custom_target(Scripts SOURCES ${LUAFiles})

FILE(GLOB_RECURSE ShaderFiles "Shaders/*.shader")
add_custom_target(Shaders SOURCES ${ShaderFiles})

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/)

include_directories(after
   Source
   ${LEADWERKS_PATH}/Include/Libraries/VA
   ${LEADWERKS_PATH}/Include/Libraries/VHACD/src/VHACD_Lib/inc
   ${LEADWERKS_PATH}/Include/Libraries/NewtonDynamics/coreLibrary_300/source/core
   ${LEADWERKS_PATH}/Include/Libraries/NewtonDynamics/coreLibrary_300/source/meshUtil
   ${LEADWERKS_PATH}/Include/Libraries/NewtonDynamics/coreLibrary_300/source/newton
   ${LEADWERKS_PATH}/Include/Libraries/NewtonDynamics/coreLibrary_300/source/physics
   ${LEADWERKS_PATH}/Include/Libraries/NewtonDynamics/packages/dMath
   ${LEADWERKS_PATH}/Include/Libraries/NewtonDynamics/packages/dContainers
   ${LEADWERKS_PATH}/Include/Libraries/NewtonDynamics/packages/dCustomJoints
   ${LEADWERKS_PATH}/Include/Libraries/tolua++-1.0.93/include
   ${LEADWERKS_PATH}/Include/Libraries/lua-5.1.4
   ${LEADWERKS_PATH}/Include/Libraries/freetype-2.4.7/include
   ${LEADWERKS_PATH}/Include/Libraries/enet-1.3.1/include
   ${LEADWERKS_PATH}/Include/Libraries/RecastNavigation/DebugUtils/Include
   ${LEADWERKS_PATH}/Include/Libraries/RecastNavigation/Detour/Include
   ${LEADWERKS_PATH}/Include/Libraries/RecastNavigation/DetourCrowd/Include
   ${LEADWERKS_PATH}/Include/Libraries/RecastNavigation/DetourTileCache/Include
   ${LEADWERKS_PATH}/Include/Libraries/RecastNavigation/Recast/Include
   ${LEADWERKS_PATH}/Include/
   ${LEADWERKS_PATH}/Include/Libraries/zlib-1.2.5
   ${LEADWERKS_PATH}/Include/Libraries/zlib-1.2.5/contrib/minizip
   ${LEADWERKS_PATH}/Include/Libraries/freetype-2.4.7/include/freetype
   ${LEADWERKS_PATH}/Include/Libraries/freetype-2.4.7/include/freetype/config
   ${LEADWERKS_PATH}/Include/Libraries/LuaJIT/dynasm
   ${LEADWERKS_PATH}/Include/Libraries/glew-1.6.0/include
)

if (CMAKE_SYSTEM_NAME STREQUAL "Linux")
   set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Wall -fexceptions -msse3 -DDG_DISABLE_ASSERT")
   set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DZLIB -DPLATFORM_LINUX -D_NEWTON_STATIC_LIB -DFT2_BUILD_LIBRARY")
   set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DOPENGL -Dunix -D__STEAM__ -D_POSIX_VER -D_POSIX_VER_64")
   set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DDG_THREAD_EMULATION -D_STATICLIB -DDG_USE_THREAD_EMULATION")
   set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DGL_GLEXT_PROTOTYPES -DLEADWERKS_3_1 -D_GLIBCXX_USE_CXX11_ABI=0")
   set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DLUA_USE_LINUX -D_CUSTOM_JOINTS_STATIC_LIB")
   set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-unknown-pragmas -Wno-unused-variable")

   if (CMAKE_BUILD_TYPE STREQUAL "Release")
    set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -s -O2")
    set (LEADWERKS_LIB ${LEADWERKS_PATH}/Library/Linux/Release/Leadwerks.a)
    set (BIN_NAME ${PROJECT_NAME})
   else(CMAKE_BUILD_TYPE STREQUAL "Release")
    set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -DDEBUG -D_DEBUG")
    set (LEADWERKS_LIB ${LEADWERKS_PATH}/Library/Linux/Debug/Leadwerks.a)
    set (BIN_NAME ${PROJECT_NAME}.debug)
   endif(CMAKE_BUILD_TYPE STREQUAL "Release")
   set (LIBS
    ${LEADWERKS_LIB} dl openal GL GLU
    ${LEADWERKS_PATH}/Library/Linux/libluajit.a
    ${CMAKE_CURRENT_SOURCE_DIR}/libsteam_api.so
    X11 Xext Xrender Xft pthread
   )
elseif(CMAKE_SYSTEM_NAME STREQUAL "Windows")
   set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /DPSAPI_VERSION=1 /D__STEAM__ /D_CUSTOM_JOINTS_STATIC_LIB")
   set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /DFT2_BUILD_LIBRARY /DLEADWERKS_3_1 /DDG_DISABLE_ASSERT")
   set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /DWINDOWS /DWIN32 /DOS_WINDOWS /DOPENGL /DPLATFORM_WINDOWS")
   set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /D_WIN_32_VER /D_NEWTON_USE_LIB /DPTW32_STATIC_LIB /DPTW32_BUILD")
   set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /D_NEWTON_STATIC_LIB /D_LIB /DDG_USE_NORMAL_PRIORITY_THREAD")
   set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /DGLEW_STATIC /D_STATICLIB /D \"SLB_LIBRARY\" /MP")
   set (CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /MTd /DDEBUG /D_DEBUG")
   set (CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /MT")

   set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /NODEFAULTLIB:MSVCRT.lib /NODEFAULTLIB:MSVCRTD.lib")
   include_directories(after
    ${LEADWERKS_PATH}/Include/Libraries/openssl/include
    ${LEADWERKS_PATH}/Include/Libraries/glslang
   )
   link_directories(${LEADWERKS_PATH}/Library/Windows/x86)
   message("Build Type: ${CMAKE_BUILD_TYPE}")
   if (CMAKE_BUILD_TYPE STREQUAL "Release")
    message("Building Win32 Release build")
    link_directories(${LEADWERKS_PATH}/Library/Windows/x86/Release)
    set (BIN_NAME ${PROJECT_NAME})
   else()
    message("Building Win32 Debug build")
    link_directories(${LEADWERKS_PATH}/Library/Windows/x86/Debug)
    set (BIN_NAME ${PROJECT_NAME}.debug)
   endif()
   set (LIBS
    libcryptoMT.lib libsslMT.lib Rpcrt4.lib crypt32.lib
    libcurl.lib msimg32.lib lua51.lib steam_api.lib
    ws2_32.lib Glu32.lib libovrd.lib OpenGL32.lib
    winmm.lib Psapi.lib OpenAL32.lib Leadwerks.lib
   )
endif(CMAKE_SYSTEM_NAME STREQUAL "Linux")

add_executable (${BIN_NAME} ${SOURCE} ${HEADERS})
target_link_libraries(${BIN_NAME} ${LIBS})


 

EDIT: Minor changes to help clean it up some. Also tested the $ENV{HOME} and it does indeed work.

EDIT2: Added a file glob for shaders and scripts so if you are using an ide like qt-creator they will show up in the project view.

  • Upvote 1
Link to comment
Share on other sites

So while playing with this some more it seems that VS2017 can only do debug builds due to a bug on how it defines CMAKE_BUILD_TYPE.

 

 

EDIT:

 

Found a work around. Under the "cmake->change cmake settings" menu click any option and replace the file with this:

 

{
   // See https://go.microsoft.com//fwlink//?linkid=834763 for more information about this file.
   "configurations": [
    {
    "name": "x86-Debug",
    "generator": "Visual Studio 15 2017",
    "configurationType" : "Debug",
    "buildRoot":  "${env.LOCALAPPDATA}\\CMakeBuild\\${workspaceHash}\\build\\${name}",
    "cmakeCommandArgs":  "-DCMAKE_BUILD_TYPE=Debug",
    "buildCommandArgs": "-m -v:minimal"
    },
  {
    "name": "x86-Release",
    "generator": "Visual Studio 15 2017",
    "configurationType": "Release",
    "buildRoot": "${env.LOCALAPPDATA}\\CMakeBuild\\${workspaceHash}\\build\\${name}",
    "cmakeCommandArgs": "-DCMAKE_BUILD_TYPE=Release",
    "buildCommandArgs": "-m -v:minimal"
  },
  {
    "name": "x64-Debug",
    "generator": "Visual Studio 15 2017 Win64",
    "configurationType": "Debug",
    "buildRoot": "${env.LOCALAPPDATA}\\CMakeBuild\\${workspaceHash}\\build\\${name}",
    "cmakeCommandArgs": "-DCMAKE_BUILD_TYPE=Debug",
    "buildCommandArgs": "-m -v:minimal"
  },
  {
    "name": "x64-Release",
    "generator": "Visual Studio 15 2017 Win64",
    "configurationType": "Release",
    "buildRoot": "${env.LOCALAPPDATA}\\CMakeBuild\\${workspaceHash}\\build\\${name}",
    "cmakeCommandArgs": "-DCMAKE_BUILD_TYPE=Release",
    "buildCommandArgs": "-m -v:minimal"
  }
   ]
}

 

The only thing changed is the cmakeCommandArgs where we pass in the correct way to define the build type.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...