Home | History | Annotate | Download | only in cmake
      1 # Boilerplate.
      2 cmake_minimum_required (VERSION 3.1)   # First version with CMAKE_CXX_STANDARD.
      3 project (skimake)
      4 set (CMAKE_CXX_STANDARD 11)
      5 
      6 # Default to Release mode.  We're mainly targeting Skia users, not Skia developers.
      7 if (NOT CMAKE_BUILD_TYPE)
      8     set (CMAKE_BUILD_TYPE Release)
      9 endif ()
     10 
     11 # To first approximation, the Skia library comprises all .cpp files under src/.
     12 file (GLOB_RECURSE srcs ../src/*.cpp)
     13 
     14 function (find_include_dirs out)
     15     file (GLOB_RECURSE headers ${ARGN})
     16     foreach (path ${headers})
     17         get_filename_component (dir ${path} PATH)
     18         list (APPEND include_dirs ${dir})
     19     endforeach()
     20     list (REMOVE_DUPLICATES include_dirs)
     21     set (${out} ${include_dirs} PARENT_SCOPE)
     22 endfunction()
     23 
     24 # We need src/ directories and include/private on our path when building Skia.
     25 # Users should be able to use Skia with only include/ directories that are not include/private.
     26 find_include_dirs(private_includes ../src/*.h ../include/private/*.h)
     27 find_include_dirs(public_includes ../include/*.h)
     28 list (REMOVE_ITEM public_includes ${private_includes})  # Easiest way to exclude private.
     29 file (GLOB default_include_config "../include/config")
     30 list (REMOVE_ITEM public_includes ${default_include_config})
     31 set (userconfig_directory ${CMAKE_BINARY_DIR}/include)
     32 list (APPEND public_includes ${userconfig_directory})
     33 
     34 # These guys are third_party but provided by a Skia checkout.
     35 list (APPEND srcs             ../third_party/etc1/etc1.cpp ../third_party/ktx/ktx.cpp)
     36 list (APPEND private_includes ../third_party/etc1          ../third_party/ktx)
     37 
     38 function (remove_srcs)
     39     file (GLOB_RECURSE to_remove ${ARGN})
     40     list (REMOVE_ITEM srcs ${to_remove})
     41     set (srcs ${srcs} PARENT_SCOPE)
     42 endfunction()
     43 
     44 # This file is empty and is only used to trick GYP.
     45 remove_srcs (../src/core/SkForceCPlusPlusLinking.cpp)
     46 # This file forces linking for all our supported image decoders.  We're more fine-grained.
     47 remove_srcs (../src/images/SkForceLinking.cpp)
     48 # Chrome only?
     49 remove_srcs (../src/ports/SkFontHost_fontconfig.cpp
     50              ../src/fonts/SkFontMgr_fontconfig.cpp
     51              ../src/ports/SkFontConfigInterface_direct.cpp
     52              ../src/ports/SkFontConfigInterface_direct_factory.cpp
     53              ../src/ports/SkFontConfigInterface_direct_google3.cpp
     54              ../src/ports/SkFontConfigInterface_direct_google3_factory.cpp)
     55 # Alternative font managers.
     56 remove_srcs (../src/ports/SkFontMgr_custom*.cpp)
     57 
     58 # Skia sure ships a lot of code no one uses.
     59 remove_srcs (../src/animator/* ../src/*nacl* ../src/svg/* ../src/views/* ../src/xml/*)
     60 foreach (include animator svg svg/parser views views/animated xml)
     61   file (GLOB globed_include ../include/${include})
     62   list (REMOVE_ITEM public_includes ${globed_include})
     63 endforeach()
     64 
     65 # Remove OS-specific source files.
     66 if (NOT UNIX)
     67     remove_srcs(../src/ports/*_posix.cpp
     68                 ../src/ports/SkTLS_pthread.cpp
     69                 ../src/ports/SkTime_Unix.cpp
     70                 ../src/utils/SkThreadUtils_pthread.cpp)
     71 endif()
     72 if (APPLE OR NOT UNIX)
     73     remove_srcs(../src/gpu/gl/glx/*
     74                 ../src/images/SkImageDecoder_FactoryDefault.cpp
     75                 ../src/ports/SkFontMgr_fontconfig*.cpp
     76                 ../src/ports/SkFontMgr_android*.cpp
     77                 ../src/*FreeType*)
     78 endif()
     79 
     80 # Remove processor-specific source files.
     81 if (NOT CMAKE_SYSTEM_PROCESSOR STREQUAL ARM)
     82     remove_srcs(../src/*arm* ../src/*ARM* ../src/*neon* ../src/*NEON*)
     83 endif()
     84 if (NOT CMAKE_SYSTEM_PROCESSOR STREQUAL MIPS)
     85     remove_srcs(../src/*mips* ../src/*MIPS*)
     86 endif()
     87 
     88 # Make our ports choices.
     89 remove_srcs(
     90     ../src/*moz*   # We're probably not Mozilla.
     91     ../src/gpu/GrContextFactory.cpp                  # For internal testing only.
     92     ../src/gpu/gl/GrGLCreateNativeInterface_none.cpp
     93     ../src/gpu/gl/GrGLDefaultInterface_none.cpp
     94     ../src/gpu/gl/SkCreatePlatformGLContext*.cpp     # For internal testing only.
     95     ../src/gpu/gl/command_buffer/*
     96     ../src/gpu/gl/egl/*
     97     ../src/gpu/gl/iOS/*
     98     ../src/gpu/vk/*
     99     ../src/opts/SkBitmapProcState_opts_none.cpp
    100     ../src/opts/SkBlitMask_opts_none.cpp
    101     ../src/opts/SkBlitRow_opts_none.cpp
    102     ../src/ports/SkFontMgr_empty_factory.cpp
    103     ../src/ports/SkGlobalInitialization_chromium.cpp
    104     ../src/ports/SkImageDecoder_empty.cpp
    105     ../src/ports/SkImageGenerator_none.cpp
    106     ../src/ports/SkTLS_none.cpp)
    107 
    108 if (WIN32)
    109     if(SKIA_GDI)
    110         remove_srcs(../src/ports/SkFontMgr_win_dw_factory.cpp)
    111     else()
    112         remove_srcs(../src/ports/SkFontMgr_win_gdi_factory.cpp)
    113     endif()
    114 endif()
    115 
    116 remove_srcs(../src/gpu/gl/angle/*)  # TODO
    117 
    118 # Certain files must be compiled with support for SSSE3, SSE4.1, AVX, or AVX2 intrinsics.
    119 file (GLOB_RECURSE ssse3_srcs ../src/*ssse3*.cpp ../src/*SSSE3*.cpp)
    120 file (GLOB_RECURSE sse41_srcs ../src/*sse4*.cpp ../src/*SSE4*.cpp)
    121 file (GLOB_RECURSE avx_srcs   ../src/*_avx.cpp)
    122 file (GLOB_RECURSE avx2_srcs  ../src/*_avx2.cpp)
    123 set_source_files_properties(${ssse3_srcs} PROPERTIES COMPILE_FLAGS -mssse3)
    124 set_source_files_properties(${sse41_srcs} PROPERTIES COMPILE_FLAGS -msse4.1)
    125 set_source_files_properties(${avx_srcs}   PROPERTIES COMPILE_FLAGS -mavx)
    126 set_source_files_properties(${avx2_srcs}  PROPERTIES COMPILE_FLAGS -mavx2)
    127 
    128 # Detect our optional dependencies.
    129 # If we can't find them, don't build the parts of Skia that use them.
    130 find_package (EXPAT)
    131 find_package (Lua)
    132 find_package (ZLIB)
    133 # No find_package for libwebp as far as I can tell, so simulate it here.
    134 find_path (WEBP_INCLUDE_DIRS "webp/decode.h")
    135 find_library (WEBP_LIBRARIES webp)
    136 find_path (OSMESA_INCLUDE_DIRS "GL/osmesa.h")
    137 find_library(OSMESA_LIBRARIES "OSMesa")
    138 
    139 if (UNIX AND NOT APPLE)
    140     find_package (Freetype)
    141     # Same deal for fontconfig.
    142     find_path (FONTCONFIG_INCLUDE_DIRS "fontconfig/fontconfig.h")
    143     find_library (FONTCONFIG_LIBRARIES fontconfig)
    144     find_package (GIF)
    145     find_package (JPEG)
    146     find_package (PNG)
    147 endif()
    148 
    149 # Do not compile SkRawCodec.
    150 remove_srcs(../src/codec/*Raw*.cpp)
    151 
    152 # TODO: macro away this if (found) ... else() ... endif() stuff.
    153 
    154 if (EXPAT_FOUND)
    155     list (APPEND private_includes ${EXPAT_INCLUDE_DIRS})
    156     list (APPEND libs             ${EXPAT_LIBRARIES})
    157 else()
    158     remove_srcs (../src/ports/SkFontMgr_android_parser.cpp)
    159 endif()
    160 
    161 if (GIF_FOUND)
    162     list (APPEND private_includes ${GIF_INCLUDE_DIRS})
    163     list (APPEND libs             ${GIF_LIBRARIES})
    164     add_definitions(-DSK_CODEC_DECODES_GIF)
    165 else()
    166     remove_srcs(../src/images/*gif*)
    167     remove_srcs(../src/codec/*Gif*)
    168 endif()
    169 
    170 if (JPEG_FOUND)
    171     list (APPEND private_includes ${JPEG_INCLUDE_DIRS})
    172     list (APPEND libs             ${JPEG_LIBRARIES})
    173     add_definitions(-DSK_CODEC_DECODES_JPEG)
    174 else()
    175     remove_srcs(../src/images/*jpeg*)
    176     remove_srcs(../src/images/*Jpeg*)
    177     remove_srcs(../src/codec/*Jpeg*)
    178 endif()
    179 
    180 if (LUA_FOUND)
    181     list (APPEND private_includes ${LUA_INCLUDE_DIR})
    182     list (APPEND libs             ${LUA_LIBRARIES})
    183 else()
    184     remove_srcs(../src/utils/*Lua*)
    185 endif()
    186 
    187 if (PNG_FOUND)
    188     list (APPEND private_includes ${PNG_INCLUDE_DIRS})
    189     list (APPEND libs             ${PNG_LIBRARIES})
    190     add_definitions(-DPNG_SKIP_SETJMP_CHECK)
    191     add_definitions(-DPNG_SKIP_SKIA_OPTS)
    192     add_definitions(-DSK_CODEC_DECODES_PNG)
    193 else()
    194     remove_srcs(../src/images/*png*)
    195     remove_srcs(../src/images/*ico*)
    196     remove_srcs(../src/codec/*Png*)
    197     remove_srcs(../src/codec/*Ico*)
    198 endif()
    199 
    200 if (ZLIB_FOUND)
    201     list (APPEND private_includes ${ZLIB_INCLUDE_DIRS})
    202     list (APPEND libs             ${ZLIB_LIBRARIES})
    203     remove_srcs(../src/doc/SkDocument_PDF_None.cpp)
    204 else()
    205     remove_srcs(../src/pdf/*.cpp ../src/doc/SkDocument_PDF.cpp)
    206 endif()
    207 
    208 if (WEBP_INCLUDE_DIRS AND WEBP_LIBRARIES)
    209     list (APPEND private_includes ${WEBP_INCLUDE_DIRS})
    210     list (APPEND libs             ${WEBP_LIBRARIES})
    211     add_definitions(-DSK_CODEC_DECODES_WEBP)
    212 else()
    213     remove_srcs(../src/images/*webp*)
    214     remove_srcs(../src/codec/*Webp*)
    215 endif()
    216 
    217 if (FREETYPE_FOUND)
    218     list (APPEND private_includes ${FREETYPE_INCLUDE_DIRS})
    219     list (APPEND libs             ${FREETYPE_LIBRARIES})
    220 endif()
    221 
    222 if (FONTCONFIG_INCLUDE_DIRS AND FONTCONFIG_LIBRARIES)
    223     list (APPEND private_includes ${FONTCONFIG_INCLUDE_DIRS})
    224     list (APPEND libs             ${FONTCONFIG_LIBRARIES})
    225 endif()
    226 
    227 if (APPLE)
    228     find_library(APPLICATION_SERVICES_FRAMEWORK ApplicationServices REQUIRED)
    229     list (APPEND libs ${APPLICATION_SERVICES_FRAMEWORK})
    230 endif()
    231 
    232 if (OSMESA_LIBRARIES AND OSMESA_INCLUDE_DIRS)
    233     list (APPEND libs             ${OSMESA_LIBRARIES})
    234     list (APPEND private_includes ${OSMESA_INCLUDE_DIRS})
    235     list (APPEND public_defines   "-DSK_MESA=1")
    236     set (SK_MESA 1)
    237 else()
    238     remove_srcs(../src/gpu/gl/mesa/*)
    239 endif()
    240 
    241 if (WIN32)
    242     list (APPEND libs FontSub.lib Usp10.lib)
    243 endif()
    244 
    245 find_package(OpenGL REQUIRED)
    246 list (APPEND libs  ${OPENGL_LIBRARIES})
    247 
    248 # This is our main output, libskia.so.
    249 # We mostly build an .so here because it helps test we've linked everything,
    250 # not so much that we think Skia is a good candidate to ship as a shared library.
    251 add_library (skia SHARED ${srcs})
    252 
    253 target_compile_definitions(skia
    254     PUBLIC  ${public_defines}
    255     PRIVATE -DSKIA_DLL -DSKIA_IMPLEMENTATION=1)
    256 
    257 target_include_directories(skia
    258     PUBLIC  ${public_includes}
    259     PRIVATE ${private_includes})
    260 
    261 target_link_libraries(skia
    262     PUBLIC
    263     PRIVATE ${libs})
    264 
    265 if (MSVC)
    266     set(cc_flags "/w /GR-")
    267 else()
    268     set(cc_flags "-w -fno-rtti -fno-exceptions")
    269 endif()
    270 
    271 set_target_properties(skia PROPERTIES
    272     COMPILE_FLAGS ${cc_flags}
    273     CXX_VISIBILITY_PRESET hidden
    274     VISIBILITY_INLINES_HIDDEN true)
    275 
    276 # Experimental C API install:
    277 file(GLOB c_headers "../include/c/*.h")
    278 install(FILES ${c_headers} DESTINATION include)
    279 install(TARGETS skia DESTINATION lib)
    280 
    281 # SkUserConfig.h
    282 if (CMAKE_BUILD_TYPE STREQUAL Release)
    283     set (SK_RELEASE 1)
    284 else()
    285     set (SK_RELEASE 0)
    286 endif()
    287 configure_file ("SkUserConfig.h.in" "${userconfig_directory}/SkUserConfig.h")
    288 
    289 # skia_link_arguments.txt
    290 set (link_arguments ${CMAKE_BINARY_DIR}/skia_link_arguments.txt)
    291 file (WRITE ${link_arguments} "-L${CMAKE_BINARY_DIR}\n")
    292 file (APPEND ${link_arguments} "-lskia\n")
    293 file (APPEND ${link_arguments} "-Wl,-rpath,${CMAKE_BINARY_DIR}\n")
    294 
    295 # skia_compile_arguments.txt
    296 set (compile_arguments ${CMAKE_BINARY_DIR}/skia_compile_arguments.txt)
    297 file (WRITE ${compile_arguments} "--std=c++11\n")
    298 foreach (include ${public_includes})
    299   get_filename_component (abs_include ${include} ABSOLUTE)
    300   file (APPEND ${compile_arguments} "-I${abs_include}\n")
    301 endforeach()
    302 
    303 # cmake .
    304 # cmake --build . --target skia
    305 # c++ -c @skia_compile_arguments.txt example.cpp
    306 # c++ example.o @skia_link_arguments.txt
    307 
    308 # skia.h
    309 set (bad_files GrGLConfig_chrome.h SkJSONCPP.h SkParsePaint.h)
    310 # make `c++ @skia_compile_arguments.txt include/skia.h` work.
    311 set (skia_h_path ${userconfig_directory}/skia.h)
    312 file (WRITE ${skia_h_path} "// skia.h generated by CMake.\n")
    313 file(APPEND ${skia_h_path} "#ifndef skia_DEFINED\n")
    314 file(APPEND ${skia_h_path} "#define skia_DEFINED\n")
    315 foreach (include ${public_includes})
    316   if (NOT include STREQUAL userconfig_directory)
    317     file (APPEND ${skia_h_path} "\n")
    318     file (GLOB all_public_headers ${include}/*.h)
    319     foreach (public_header ${all_public_headers})
    320       get_filename_component (filename_component ${public_header} NAME)
    321       if (NOT ";${bad_files};" MATCHES ";${filename_component};")
    322         file (APPEND ${skia_h_path} "#include \"${filename_component}\"\n")
    323       endif ()
    324     endforeach()
    325   endif()
    326 endforeach()
    327 file(APPEND ${skia_h_path} "\n#endif  // skia_DEFINED\n")
    328 
    329 # Now build a simple example app that uses Skia via libskia.so.
    330 add_executable(example example.cpp)
    331 target_link_libraries(example skia ${OPENGL_LIBRARIES})
    332