开发者

CMakelists.txt is ridiculously complex to get windows and mac to work, is there a better way?

I have been getting a CMakeLists.txt together to compile what right now is an SFML sample in preparation to do my own source code. It feels like a hack, even though it works (Mac Makefile, VS nmake, VS solution) right now.

The main repository is at https://github.com/iaefai/Spider-Fish/

Any 开发者_Go百科suggestions are welcome.

cmake_minimum_required(VERSION 2.8)

PROJECT(Spider-Fish)

FIND_PACKAGE(OpenGL REQUIRED)
FIND_PACKAGE(SFML REQUIRED)


IF (WIN32)
    # Windows

    link_directories(${SFML_INCLUDE_DIR}/../lib)

    set(RESOURCE_HANDLER ${CMAKE_CURRENT_SOURCE_DIR}/src/windows/resources.cpp)
#   link_directories(${FIND_SFML_LIB_PATHS})

ELSEIF(APPLE)
        # Mac
        SET(RESOURCE_HANDLER ${CMAKE_CURRENT_SOURCE_DIR}/src/mac/resources.mm)
        SET(MACOSX_BUNDLE_GUI_IDENTIFER "com.iaefai.Spider-Fish")
        SET(MACOSX_BUNDLE_BUNDLE_NAME ${PROJECT_NAME})
        if (NOT CMAKE_OSX_ARCHITECTURES)
            set(CMAKE_OSX_ARCHITECTURES "i386;x86_64"
                CACHE STRING "Build architectures for OSX" FORCE)
        endif()

        if(EXISTS /Developer/SDKs/MacOSX10.7.sdk)
            set(CMAKE_OSX_SYSROOT "/Developer/SDKs/MacOSX10.7.sdk"
                CACHE STRING "Defaults to 10.7" FORCE)
        else()
            # use default SDK
        endif()           

        find_library(COCOA_LIB  Cocoa)

        SET(EXTRA_LIBS ${COCOA_LIB} ${OPENGL_LIBRARIES})
ELSE()
        # Linux // Assumed??
ENDIF()

#include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include})

include_directories(${SFML_INCLUDE_DIR})

SET(RESOURCES   ${CMAKE_CURRENT_SOURCE_DIR}/assets/background.jpg 
                ${CMAKE_CURRENT_SOURCE_DIR}/assets/blur.sfx
                ${CMAKE_CURRENT_SOURCE_DIR}/assets/colorize.sfx
                ${CMAKE_CURRENT_SOURCE_DIR}/assets/edge.sfx
                ${CMAKE_CURRENT_SOURCE_DIR}/assets/fisheye.sfx
                ${CMAKE_CURRENT_SOURCE_DIR}/assets/nothing.sfx
                ${CMAKE_CURRENT_SOURCE_DIR}/assets/pixelate.sfx
                ${CMAKE_CURRENT_SOURCE_DIR}/assets/sansation.ttf
                ${CMAKE_CURRENT_SOURCE_DIR}/assets/sprite.png
                ${CMAKE_CURRENT_SOURCE_DIR}/assets/wave.jpg
                ${CMAKE_CURRENT_SOURCE_DIR}/assets/wave.sfx)


SET (SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/src/Shader.cpp)
#SET (HEADERS include/resources.h)


add_executable(${PROJECT_NAME} ${SOURCES} ${RESOURCE_HANDLER})

The biggest hack seems to be the stuff to copy resources. That would be ideal to have a special command that could do that on multiplatform. Not entirely certain how to do that — I suspect a set_target_resources would be a good name.

IF (APPLE)

    set_target_properties(${PROJECT_NAME} 
        PROPERTIES
            MACOSX_BUNDLE_INFO_PLIST ${CMAKE_CURRENT_SOURCE_DIR}/src/mac/Spider-Fish-Info.plist
            MACOSX_BUNDLE  TRUE)      

#    add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
    add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD 
        COMMAND echo copying resources... 
        #${RESOURCES}
        COMMAND mkdir -p ./${PROJECT_NAME}.app/Contents/Resources
        COMMAND cp ${RESOURCES} ./${PROJECT_NAME}.app/Contents/Resources                
    )
#    add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD COMMAND echo ${PROJECT_NAME}.app/Contents/Resources)


ENDIF (APPLE)

IF (WIN32)
    set_target_properties(${PROJECT_NAME}
        PROPERTIES
            WIN32_EXECUTABLE FALSE)

I would worry about this custom stuff on windows because of how specialized it is getting.

    set(EXTRA_LIBS sfml-main ${OPENGL_LIBRARIES})
    # note we can add stuff to ADDITIONAL_MAKE_CLEAN_FILES 
    string(COMPARE EQUAL ${CMAKE_GENERATOR} "NMake Makefiles" NMAKE)
    if (NMAKE)
        add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
            COMMAND echo copying dlls...
            COMMAND copy ${SFML_INCLUDE_DIR}\\..\\bin\\*.dll .
            COMMAND echo copying resources... from ${CMAKE_CURRENT_SOURCE_DIR}
            COMMAND -mkdir resources 
            COMMAND copy \"${CMAKE_CURRENT_SOURCE_DIR}\\assets\\*.*\" resources)
    else ()
        add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
            COMMAND echo copying dlls...
            COMMAND copy ${SFML_INCLUDE_DIR}\\..\\bin\\*.dll .
            COMMAND echo copying resources... from ${CMAKE_CURRENT_SOURCE_DIR}
            # Visual Studio does not support the '-' to ignore errors
            COMMAND rmdir /s /q resources
            COMMAND mkdir resources 
            COMMAND copy \"${CMAKE_CURRENT_SOURCE_DIR}\\assets\\*.*\" resources)
    endif (NMAKE)
ENDIF (WIN32)

#if (APPLE)
target_link_libraries(${PROJECT_NAME} 
    sfml-system 
    sfml-window
    sfml-network
    sfml-graphics
    sfml-audio
    ${EXTRA_LIBS})
#endif (APPLE)


Have you tried using the INSTALL command instead of the custom post build steps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜