CMakeLists.txt
author Ryan C. Gordon <icculus@icculus.org>
Fri, 27 May 2016 14:02:59 -0400
changeset 1162 de0613aa8f07
parent 1150 02c0f0afb39a
child 1166 ba0794ea4172
permissions -rw-r--r--
A few more Visual Studio warning fixes.

CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
PROJECT(MojoShader)

OPTION(BUILD_SHARED "Build MojoShader as a shared library" OFF)
OPTION(PROFILE_D3D "Build MojoShader with support for the D3D profile" ON)
OPTION(PROFILE_BYTECODE "Build MojoShader with support for the BYTECODE profile" ON)
OPTION(PROFILE_GLSL120 "Build MojoShader with support for the GLSL120 profile" ON)
OPTION(PROFILE_GLSL "Build MojoShader with support for the GLSL profile" ON)
OPTION(PROFILE_ARB1 "Build MojoShader with support for the ARB1 profile" ON)
OPTION(PROFILE_ARB1_NV "Build MojoShader with support for the ARB1_NV profile" ON)
OPTION(EFFECT_SUPPORT "Build MojoShader with support for Effect framework files" OFF)
OPTION(FLIP_VIEWPORT "Build MojoShader with the ability to flip the GL viewport" OFF)
OPTION(DEPTH_CLIPPING "Build MojoShader with the ability to simulate [0, 1] depth clipping" OFF)
OPTION(XNA4_VERTEXTEXTURE "Build MojoShader with XNA4 vertex texturing behavior" OFF)

INCLUDE_DIRECTORIES(.)

FIND_PROGRAM(HG hg DOC "Path to hg command line app: http://www.selenic.com/mercurial/")
IF(NOT HG)
    MESSAGE(STATUS "Mercurial (hg) not found. You can go on, but version info will be wrong.")
    SET(MOJOSHADER_VERSION -1)
    SET(MOJOSHADER_CHANGESET "???")
ELSE(NOT HG)
    MARK_AS_ADVANCED(HG)
    EXECUTE_PROCESS(
        COMMAND hg tip --template {rev}
        WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
        RESULT_VARIABLE HGVERSION_RC
        OUTPUT_VARIABLE MOJOSHADER_VERSION
        ERROR_QUIET
        OUTPUT_STRIP_TRAILING_WHITESPACE
    )
    EXECUTE_PROCESS(
        COMMAND hg tip --template hg-{rev}:{node|short}
        WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
        RESULT_VARIABLE HGVERSION_RC
        OUTPUT_VARIABLE MOJOSHADER_CHANGESET
        ERROR_QUIET
        OUTPUT_STRIP_TRAILING_WHITESPACE
    )
ENDIF(NOT HG)

WRITE_FILE(
    "${CMAKE_CURRENT_SOURCE_DIR}/mojoshader_version.h"
    "/* This file was autogenerated. Do not edit! */\n"
    "#ifndef _INCL_MOJOSHADER_VERSION_H_\n"
    "#define _INCL_MOJOSHADER_VERSION_H_\n"
    "#define MOJOSHADER_VERSION ${MOJOSHADER_VERSION}\n"
    "#define MOJOSHADER_CHANGESET \"${MOJOSHADER_CHANGESET}\"\n"
    "#endif\n"
)

IF(CMAKE_COMPILER_IS_GNUCC)
    ADD_DEFINITIONS(-Wall -ggdb3)
ENDIF(CMAKE_COMPILER_IS_GNUCC)

# testparse uses this when I'm looking at memory usage patterns.
#ADD_DEFINITIONS(-DMOJOSHADER_DEBUG_MALLOC=1)

IF(MSVC)
    ADD_DEFINITIONS(-D_CRT_SECURE_NO_WARNINGS=1)
    ADD_DEFINITIONS(-TP)  # force .c files to compile as C++.
ENDIF(MSVC)

# We build lemon, then use it to generate parser C code.
ADD_EXECUTABLE(lemon "misc/lemon.c")
GET_TARGET_PROPERTY(LEMON lemon LOCATION)
ADD_CUSTOM_COMMAND(
    OUTPUT "${CMAKE_CURRENT_SOURCE_DIR}/mojoshader_parser_hlsl.h"
    MAIN_DEPENDENCY "${CMAKE_CURRENT_SOURCE_DIR}/mojoshader_parser_hlsl.lemon"
    DEPENDS lemon "${CMAKE_CURRENT_SOURCE_DIR}/misc/lempar.c"
    COMMAND "${LEMON}"
    ARGS -q "-T${CMAKE_CURRENT_SOURCE_DIR}/misc/lempar.c" "${CMAKE_CURRENT_SOURCE_DIR}/mojoshader_parser_hlsl.lemon"
)

IF(APPLE)
    find_library(CARBON_FRAMEWORK Carbon)  # Stupid Gestalt.
ENDIF(APPLE)

IF(NOT PROFILE_D3D)
    ADD_DEFINITIONS(-DSUPPORT_PROFILE_D3D=0)
ENDIF(NOT PROFILE_D3D)
IF(NOT PROFILE_BYTECODE)
    ADD_DEFINITIONS(-DSUPPORT_PROFILE_BYTECODE=0)
ENDIF(NOT PROFILE_BYTECODE)
IF(NOT PROFILE_GLSL120)
    ADD_DEFINITIONS(-DSUPPORT_PROFILE_GLSL120=0)
ENDIF(NOT PROFILE_GLSL120)
IF(NOT PROFILE_GLSL)
    ADD_DEFINITIONS(-DSUPPORT_PROFILE_GLSL=0)
ENDIF(NOT PROFILE_GLSL)
IF(NOT PROFILE_ARB1)
    ADD_DEFINITIONS(-DSUPPORT_PROFILE_ARB1=0)
ENDIF(NOT PROFILE_ARB1)
IF(NOT PROFILE_ARB1_NV)
    ADD_DEFINITIONS(-DSUPPORT_PROFILE_ARB1_NV=0)
ENDIF(NOT PROFILE_ARB1_NV)

IF(EFFECT_SUPPORT)
    IF(UNIX)
        SET(LIBM -lm)
    ENDIF(UNIX)
    ADD_DEFINITIONS(-DMOJOSHADER_EFFECT_SUPPORT)
ENDIF(EFFECT_SUPPORT)

IF(FLIP_VIEWPORT)
    ADD_DEFINITIONS(-DMOJOSHADER_FLIP_RENDERTARGET)
ENDIF(FLIP_VIEWPORT)

IF(DEPTH_CLIPPING)
    ADD_DEFINITIONS(-DMOJOSHADER_DEPTH_CLIPPING)
ENDIF(DEPTH_CLIPPING)

IF(XNA4_VERTEXTEXTURE)
    ADD_DEFINITIONS(-DMOJOSHADER_XNA4_VERTEX_TEXTURES)
ENDIF(XNA4_VERTEXTEXTURE)

IF(BUILD_SHARED)
    SET(LIBRARY_FORMAT SHARED)
ELSE(BUILD_SHARED)
    SET(LIBRARY_FORMAT STATIC)
ENDIF(BUILD_SHARED)

ADD_LIBRARY(mojoshader ${LIBRARY_FORMAT}
    mojoshader.c
    mojoshader_common.c
    mojoshader_effects.c
    mojoshader_compiler.c
    mojoshader_preprocessor.c
    mojoshader_lexer.c
    mojoshader_assembler.c
    mojoshader_opengl.c
)
IF(BUILD_SHARED)
    TARGET_LINK_LIBRARIES(mojoshader ${LIBM} ${CARBON_FRAMEWORK})
ENDIF(BUILD_SHARED)

SET_SOURCE_FILES_PROPERTIES(
    mojoshader_compiler.c
    PROPERTIES OBJECT_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/mojoshader_parser_hlsl.h"
)

FIND_PROGRAM(RE2C re2c DOC "Path to re2c command line app: http://re2c.org/")
IF(NOT RE2C)
    MESSAGE(STATUS "re2c missing. You can go on, but can't rebuild the lexer.")
ELSE(NOT RE2C)
    MARK_AS_ADVANCED(RE2C)
    ADD_CUSTOM_COMMAND(
        OUTPUT "${CMAKE_CURRENT_SOURCE_DIR}/mojoshader_lexer.c"
        DEPENDS mojoshader_lexer.re
        COMMAND "${RE2C}"
        ARGS -is --no-generation-date -o "${CMAKE_CURRENT_SOURCE_DIR}/mojoshader_lexer.c" "${CMAKE_CURRENT_SOURCE_DIR}/mojoshader_lexer.re"
    )
ENDIF(NOT RE2C)

find_path(SDL2_INCLUDE_DIR SDL.h PATH_SUFFIXES include/SDL2)
find_library(SDL2 NAMES SDL2 PATH_SUFFIXES lib)
IF(SDL2)
    INCLUDE_DIRECTORIES(${SDL2_INCLUDE_DIR})
    ADD_EXECUTABLE(glcaps utils/glcaps.c)
    TARGET_LINK_LIBRARIES(glcaps ${SDL2} ${LIBM} ${CARBON_FRAMEWORK})
    ADD_EXECUTABLE(bestprofile utils/bestprofile.c)
    TARGET_LINK_LIBRARIES(bestprofile mojoshader ${SDL2} ${LIBM} ${CARBON_FRAMEWORK})
    ADD_EXECUTABLE(availableprofiles utils/availableprofiles.c)
    TARGET_LINK_LIBRARIES(availableprofiles mojoshader ${SDL2} ${LIBM} ${CARBON_FRAMEWORK})
ENDIF(SDL2)

ADD_EXECUTABLE(finderrors utils/finderrors.c)
TARGET_LINK_LIBRARIES(finderrors mojoshader ${SDL2} ${LIBM} ${CARBON_FRAMEWORK})
IF(SDL2)
    SET_SOURCE_FILES_PROPERTIES(
        utils/finderrors.c
        PROPERTIES COMPILE_FLAGS "-DFINDERRORS_COMPILE_SHADERS=1"
    )
ENDIF(SDL2)

ADD_EXECUTABLE(testparse utils/testparse.c)
TARGET_LINK_LIBRARIES(testparse mojoshader ${LIBM} ${CARBON_FRAMEWORK})
ADD_EXECUTABLE(testoutput utils/testoutput.c)
TARGET_LINK_LIBRARIES(testoutput mojoshader ${LIBM} ${CARBON_FRAMEWORK})
ADD_EXECUTABLE(mojoshader-compiler utils/mojoshader-compiler.c)
TARGET_LINK_LIBRARIES(mojoshader-compiler mojoshader ${LIBM} ${CARBON_FRAMEWORK})

# Unit tests...
ADD_CUSTOM_TARGET(
    test
    COMMAND "${CMAKE_CURRENT_SOURCE_DIR}/unit_tests/run_tests.pl"
    WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}"
    DEPENDS mojoshader-compiler
    COMMENT "Running unit tests..."
    VERBATIM
)

# End of CMakeLists.txt ...