• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1include(FetchContent)
2set(FETCHCONTENT_QUIET OFF)
3
4if(CMAKE_SYSTEM_NAME MATCHES "Windows" AND ${CMAKE_VERSION} VERSION_GREATER_EQUAL 3.17.0)
5    set(CMAKE_FIND_LIBRARY_SUFFIXES .dll ${CMAKE_FIND_LIBRARY_SUFFIXES})
6endif()
7
8function(mindspore_add_submodule_obj des_submodule_objs sub_dir submodule_name_obj)
9
10    add_subdirectory(${sub_dir})
11
12    if(NOT TARGET ${submodule_name_obj})
13        message(FATAL_ERROR "Can not find submodule '${submodule_name_obj}'. in ${CMAKE_CURRENT_LIST_FILE}")
14    endif()
15    if("$<TARGET_OBJECTS:${submodule_name_obj}>" IN_LIST ${des_submodule_objs})
16        message(FATAL_ERROR "submodule '${submodule_name_obj}' added more than once. in ${CMAKE_CURRENT_LIST_FILE}")
17    endif()
18
19    set(${des_submodule_objs} ${${des_submodule_objs}} $<TARGET_OBJECTS:${submodule_name_obj}> PARENT_SCOPE)
20
21endfunction()
22
23if(DEFINED ENV{MSLIBS_CACHE_PATH})
24    set(_MS_LIB_CACHE  $ENV{MSLIBS_CACHE_PATH})
25else()
26    set(_MS_LIB_CACHE ${CMAKE_BINARY_DIR}/.mslib)
27endif()
28message("MS LIBS CACHE PATH:  ${_MS_LIB_CACHE}")
29
30if(NOT EXISTS ${_MS_LIB_CACHE})
31    file(MAKE_DIRECTORY ${_MS_LIB_CACHE})
32endif()
33
34if(DEFINED ENV{MSLIBS_SERVER} AND NOT ENABLE_GITEE)
35    set(LOCAL_LIBS_SERVER  $ENV{MSLIBS_SERVER})
36    message("LOCAL_LIBS_SERVER:  ${LOCAL_LIBS_SERVER}")
37endif()
38
39include(ProcessorCount)
40ProcessorCount(N)
41if(JOBS)
42    set(THNUM ${JOBS})
43else()
44    set(JOBS 8)
45    if(${JOBS} GREATER ${N})
46        set(THNUM ${N})
47    else()
48        set(THNUM ${JOBS})
49    endif()
50endif()
51message("set make thread num: ${THNUM}")
52
53if(LOCAL_LIBS_SERVER)
54    if(NOT ENV{no_proxy})
55        set(ENV{no_proxy} "${LOCAL_LIBS_SERVER}")
56    else()
57        string(FIND $ENV{no_proxy} ${LOCAL_LIBS_SERVER} IP_POS)
58        if(${IP_POS} EQUAL -1)
59            set(ENV{no_proxy} "$ENV{no_proxy},${LOCAL_LIBS_SERVER}")
60        endif()
61    endif()
62endif()
63
64function(__download_pkg pkg_name pkg_url pkg_md5)
65
66    if(LOCAL_LIBS_SERVER)
67        get_filename_component(_URL_FILE_NAME ${pkg_url} NAME)
68        set(pkg_url "http://${LOCAL_LIBS_SERVER}:8081/libs/${pkg_name}/${_URL_FILE_NAME}" ${pkg_url})
69    endif()
70
71    FetchContent_Declare(
72            ${pkg_name}
73            URL      ${pkg_url}
74            URL_HASH MD5=${pkg_md5}
75    )
76    FetchContent_GetProperties(${pkg_name})
77    message("download: ${${pkg_name}_SOURCE_DIR} , ${pkg_name} , ${pkg_url}")
78    if(NOT ${pkg_name}_POPULATED)
79        FetchContent_Populate(${pkg_name})
80        set(${pkg_name}_SOURCE_DIR ${${pkg_name}_SOURCE_DIR} PARENT_SCOPE)
81    endif()
82
83endfunction()
84
85function(__download_pkg_with_git pkg_name pkg_url pkg_git_commit pkg_md5)
86
87    if(LOCAL_LIBS_SERVER)
88        set(pkg_url "http://${LOCAL_LIBS_SERVER}:8081/libs/${pkg_name}/${pkg_git_commit}")
89        FetchContent_Declare(
90                ${pkg_name}
91                URL      ${pkg_url}
92                URL_HASH MD5=${pkg_md5}
93    )
94    else()
95    FetchContent_Declare(
96            ${pkg_name}
97        GIT_REPOSITORY      ${pkg_url}
98        GIT_TAG             ${pkg_git_commit})
99    endif()
100    FetchContent_GetProperties(${pkg_name})
101    message("download: ${${pkg_name}_SOURCE_DIR} , ${pkg_name} , ${pkg_url}")
102    if(NOT ${pkg_name}_POPULATED)
103        FetchContent_Populate(${pkg_name})
104        set(${pkg_name}_SOURCE_DIR ${${pkg_name}_SOURCE_DIR} PARENT_SCOPE)
105    endif()
106
107endfunction()
108
109
110function(__find_pkg_then_add_target pkg_name pkg_exe lib_path)
111
112    unset(${pkg_name}_LIBS)
113
114    message("_FIND:${${pkg_name}_BASE_DIR}")
115
116    if(pkg_exe)
117        find_program(${pkg_exe}_EXE ${pkg_exe} PATHS ${${pkg_name}_BASE_DIR}/bin NO_DEFAULT_PATH)
118        if(NOT ${pkg_exe}_EXE)
119            return()
120        endif()
121        add_executable(${pkg_name}::${pkg_exe} IMPORTED GLOBAL)
122        set_target_properties(${pkg_name}::${pkg_exe} PROPERTIES
123                IMPORTED_LOCATION ${${pkg_exe}_EXE}
124                )
125        message("found ${${pkg_exe}_EXE}")
126    endif()
127
128    foreach(_LIB_NAME ${ARGN})
129        set(_LIB_SEARCH_NAME ${_LIB_NAME})
130        set(_LIB_TYPE SHARED)
131        if(${pkg_name}_USE_STATIC_LIBS)
132            set(_LIB_SEARCH_NAME "${CMAKE_STATIC_LIBRARY_PREFIX}${_LIB_NAME}${CMAKE_STATIC_LIBRARY_SUFFIX}")
133            set(_LIB_TYPE STATIC)
134        endif()
135        set(${_LIB_NAME}_LIB ${_LIB_NAME}_LIB-NOTFOUND)
136        if(APPLE)
137            find_library(${_LIB_NAME}_LIB ${_LIB_SEARCH_NAME} PATHS ${${pkg_name}_BASE_DIR}/${lib_path}
138                    NO_DEFAULT_PATH NO_CMAKE_FIND_ROOT_PATH)
139        else()
140            find_library(${_LIB_NAME}_LIB ${_LIB_SEARCH_NAME} PATHS ${${pkg_name}_BASE_DIR}/${lib_path}
141                    NO_DEFAULT_PATH)
142        endif()
143        if(NOT ${_LIB_NAME}_LIB)
144            return()
145        endif()
146
147        add_library(${pkg_name}::${_LIB_NAME} ${_LIB_TYPE} IMPORTED GLOBAL)
148        if(WIN32 AND ${_LIB_TYPE} STREQUAL "SHARED")
149            set_target_properties(${pkg_name}::${_LIB_NAME} PROPERTIES IMPORTED_IMPLIB_RELEASE ${${_LIB_NAME}_LIB})
150        else()
151            set_target_properties(${pkg_name}::${_LIB_NAME} PROPERTIES IMPORTED_LOCATION ${${_LIB_NAME}_LIB})
152        endif()
153
154        if(EXISTS ${${pkg_name}_BASE_DIR}/include)
155            set_target_properties(${pkg_name}::${_LIB_NAME} PROPERTIES
156                INTERFACE_INCLUDE_DIRECTORIES "${${pkg_name}_BASE_DIR}/include")
157        endif()
158
159        list(APPEND ${pkg_name}_LIBS ${pkg_name}::${_LIB_NAME})
160        message("found ${${_LIB_NAME}_LIB}")
161        STRING(REGEX REPLACE "(.+)/(.+)" "\\1" LIBPATH ${${_LIB_NAME}_LIB})
162        set(${pkg_name}_LIBPATH ${LIBPATH} CACHE STRING INTERNAL)
163    endforeach()
164
165    set(${pkg_name}_LIBS ${${pkg_name}_LIBS} PARENT_SCOPE)
166endfunction()
167
168function(__exec_cmd)
169    set(options)
170    set(oneValueArgs WORKING_DIRECTORY)
171    set(multiValueArgs COMMAND)
172
173    cmake_parse_arguments(EXEC "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
174
175    execute_process(COMMAND ${EXEC_COMMAND}
176            WORKING_DIRECTORY ${EXEC_WORKING_DIRECTORY}
177            RESULT_VARIABLE RESULT)
178    if(NOT RESULT EQUAL "0")
179        message(FATAL_ERROR "error! when ${EXEC_COMMAND} in ${EXEC_WORKING_DIRECTORY}")
180    endif()
181endfunction()
182
183function(__check_patches pkg_patches)
184    # check patches
185    if(PKG_PATCHES)
186        file(TOUCH ${_MS_LIB_CACHE}/${pkg_name}_patch.md5)
187        file(READ ${_MS_LIB_CACHE}/${pkg_name}_patch.md5 ${pkg_name}_PATCHES_MD5)
188
189        message("patches md5:${${pkg_name}_PATCHES_MD5}")
190
191        set(${pkg_name}_PATCHES_NEW_MD5)
192        foreach(_PATCH ${PKG_PATCHES})
193            file(MD5 ${_PATCH} _PF_MD5)
194            set(${pkg_name}_PATCHES_NEW_MD5 "${${pkg_name}_PATCHES_NEW_MD5},${_PF_MD5}")
195        endforeach()
196
197        if(NOT ${pkg_name}_PATCHES_MD5 STREQUAL ${pkg_name}_PATCHES_NEW_MD5)
198            set(${pkg_name}_PATCHES ${PKG_PATCHES})
199            file(REMOVE_RECURSE "${_MS_LIB_CACHE}/${pkg_name}-subbuild")
200            file(WRITE ${_MS_LIB_CACHE}/${pkg_name}_patch.md5 ${${pkg_name}_PATCHES_NEW_MD5})
201            message("patches changed : ${${pkg_name}_PATCHES_NEW_MD5}")
202        endif()
203    endif()
204endfunction()
205
206set(MS_FIND_NO_DEFAULT_PATH NO_CMAKE_PATH NO_CMAKE_ENVIRONMENT_PATH NO_SYSTEM_ENVIRONMENT_PATH
207                            NO_CMAKE_BUILDS_PATH NO_CMAKE_PACKAGE_REGISTRY NO_CMAKE_SYSTEM_PATH
208                            NO_CMAKE_SYSTEM_PACKAGE_REGISTRY)
209set(MS_FIND_NO_DEFAULT_PATH ${MS_FIND_NO_DEFAULT_PATH} PARENT_SCOPE)
210function(mindspore_add_pkg pkg_name)
211
212    set(options)
213    set(oneValueArgs URL MD5 GIT_REPOSITORY GIT_TAG VER EXE DIR HEAD_ONLY CMAKE_PATH RELEASE LIB_PATH CUSTOM_CMAKE)
214    set(multiValueArgs
215            CMAKE_OPTION LIBS PRE_CONFIGURE_COMMAND CONFIGURE_COMMAND BUILD_OPTION INSTALL_INCS
216            INSTALL_LIBS PATCHES SUBMODULES SOURCEMODULES ONLY_MAKE ONLY_MAKE_INCS ONLY_MAKE_LIBS)
217    cmake_parse_arguments(PKG "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
218
219    if(NOT PKG_LIB_PATH)
220        set(PKG_LIB_PATH lib)
221    endif()
222
223    if(NOT PKG_EXE)
224        set(PKG_EXE 0)
225    endif()
226
227    set(__FIND_PKG_NAME ${pkg_name})
228    string(TOLOWER ${pkg_name} pkg_name)
229    message("pkg name:${__FIND_PKG_NAME},${pkg_name}")
230
231    set(${pkg_name}_PATCHES_HASH)
232    foreach(_PATCH ${PKG_PATCHES})
233        file(MD5 ${_PATCH} _PF_MD5)
234        set(${pkg_name}_PATCHES_HASH "${${pkg_name}_PATCHES_HASH},${_PF_MD5}")
235    endforeach()
236
237    # check options
238    set(${pkg_name}_CONFIG_TXT
239            "${CMAKE_CXX_COMPILER_VERSION}-${CMAKE_C_COMPILER_VERSION}
240            ${ARGN} - ${${pkg_name}_USE_STATIC_LIBS}- ${${pkg_name}_PATCHES_HASH}
241            ${${pkg_name}_CXXFLAGS}--${${pkg_name}_CFLAGS}--${${pkg_name}_LDFLAGS}")
242    string(REPLACE ";" "-" ${pkg_name}_CONFIG_TXT ${${pkg_name}_CONFIG_TXT})
243    string(MD5 ${pkg_name}_CONFIG_HASH ${${pkg_name}_CONFIG_TXT})
244
245    message("${pkg_name} config hash: ${${pkg_name}_CONFIG_HASH}")
246
247    set(${pkg_name}_BASE_DIR ${_MS_LIB_CACHE}/${pkg_name}_${${pkg_name}_CONFIG_HASH})
248    set(${pkg_name}_DIRPATH ${${pkg_name}_BASE_DIR} CACHE STRING INTERNAL)
249
250    if(EXISTS ${${pkg_name}_BASE_DIR}/options.txt AND PKG_HEAD_ONLY)
251        set(${pkg_name}_INC ${${pkg_name}_BASE_DIR}/${PKG_HEAD_ONLY} PARENT_SCOPE)
252        add_library(${pkg_name} INTERFACE)
253        target_include_directories(${pkg_name} INTERFACE ${${pkg_name}_INC})
254        if(${PKG_RELEASE})
255            __find_pkg_then_add_target(${pkg_name} ${PKG_EXE} ${PKG_LIB_PATH} ${PKG_LIBS})
256        endif()
257        return()
258    endif()
259
260    set(${__FIND_PKG_NAME}_ROOT ${${pkg_name}_BASE_DIR})
261    set(${__FIND_PKG_NAME}_ROOT ${${pkg_name}_BASE_DIR} PARENT_SCOPE)
262
263    if(PKG_LIBS)
264        __find_pkg_then_add_target(${pkg_name} ${PKG_EXE} ${PKG_LIB_PATH} ${PKG_LIBS})
265        if(${pkg_name}_LIBS)
266            set(${pkg_name}_INC ${${pkg_name}_BASE_DIR}/include PARENT_SCOPE)
267            message("Found libs: ${${pkg_name}_LIBS}")
268            return()
269        endif()
270    elseif(NOT PKG_HEAD_ONLY)
271        find_package(${__FIND_PKG_NAME} ${PKG_VER} ${MS_FIND_NO_DEFAULT_PATH})
272        if(${__FIND_PKG_NAME}_FOUND)
273            set(${pkg_name}_INC ${${pkg_name}_BASE_DIR}/include PARENT_SCOPE)
274            message("Found pkg: ${__FIND_PKG_NAME}")
275            return()
276        endif()
277    endif()
278
279    if(NOT PKG_DIR)
280        if(PKG_GIT_REPOSITORY)
281            __download_pkg_with_git(${pkg_name} ${PKG_GIT_REPOSITORY} ${PKG_GIT_TAG} ${PKG_MD5})
282        else()
283            __download_pkg(${pkg_name} ${PKG_URL} ${PKG_MD5})
284        endif()
285        foreach(_SUBMODULE_FILE ${PKG_SUBMODULES})
286            STRING(REGEX REPLACE "(.+)_(.+)" "\\1" _SUBMODEPATH ${_SUBMODULE_FILE})
287            STRING(REGEX REPLACE "(.+)/(.+)" "\\2" _SUBMODENAME ${_SUBMODEPATH})
288            file(GLOB ${pkg_name}_INSTALL_SUBMODULE ${_SUBMODULE_FILE}/*)
289            file(COPY ${${pkg_name}_INSTALL_SUBMODULE} DESTINATION ${${pkg_name}_SOURCE_DIR}/3rdparty/${_SUBMODENAME})
290        endforeach()
291    else()
292        set(${pkg_name}_SOURCE_DIR ${PKG_DIR})
293    endif()
294    file(WRITE ${${pkg_name}_BASE_DIR}/options.txt ${${pkg_name}_CONFIG_TXT})
295    message("${pkg_name}_SOURCE_DIR : ${${pkg_name}_SOURCE_DIR}")
296
297    foreach(_PATCH_FILE ${PKG_PATCHES})
298        get_filename_component(_PATCH_FILE_NAME ${_PATCH_FILE} NAME)
299        set(_LF_PATCH_FILE ${CMAKE_BINARY_DIR}/_ms_patch/${_PATCH_FILE_NAME})
300        configure_file(${_PATCH_FILE} ${_LF_PATCH_FILE} NEWLINE_STYLE LF @ONLY)
301
302        message("patching ${${pkg_name}_SOURCE_DIR} -p1 < ${_LF_PATCH_FILE}")
303        execute_process(COMMAND ${Patch_EXECUTABLE} -p1 INPUT_FILE ${_LF_PATCH_FILE}
304                WORKING_DIRECTORY ${${pkg_name}_SOURCE_DIR}
305                RESULT_VARIABLE Result)
306        if(NOT Result EQUAL "0")
307            message(FATAL_ERROR "Failed patch: ${_LF_PATCH_FILE}")
308        endif()
309    endforeach()
310    foreach(_SOURCE_DIR ${PKG_SOURCEMODULES})
311        file(GLOB ${pkg_name}_INSTALL_SOURCE ${${pkg_name}_SOURCE_DIR}/${_SOURCE_DIR}/*)
312        file(COPY ${${pkg_name}_INSTALL_SOURCE} DESTINATION ${${pkg_name}_BASE_DIR}/${_SOURCE_DIR}/)
313    endforeach()
314    file(LOCK ${${pkg_name}_BASE_DIR} DIRECTORY GUARD FUNCTION RESULT_VARIABLE ${pkg_name}_LOCK_RET TIMEOUT 600)
315    if(NOT ${pkg_name}_LOCK_RET EQUAL "0")
316        message(FATAL_ERROR "error! when try lock ${${pkg_name}_BASE_DIR} : ${${pkg_name}_LOCK_RET}")
317    endif()
318
319    if(PKG_CUSTOM_CMAKE)
320        file(GLOB ${pkg_name}_cmake ${PKG_CUSTOM_CMAKE}/CMakeLists.txt)
321        file(COPY ${${pkg_name}_cmake} DESTINATION ${${pkg_name}_SOURCE_DIR})
322    endif()
323
324    if(${pkg_name}_SOURCE_DIR)
325        if(PKG_HEAD_ONLY)
326            file(GLOB ${pkg_name}_SOURCE_SUBDIRS ${${pkg_name}_SOURCE_DIR}/*)
327            file(COPY ${${pkg_name}_SOURCE_SUBDIRS} DESTINATION ${${pkg_name}_BASE_DIR})
328            set(${pkg_name}_INC ${${pkg_name}_BASE_DIR}/${PKG_HEAD_ONLY} PARENT_SCOPE)
329            if(NOT PKG_RELEASE)
330                add_library(${pkg_name} INTERFACE)
331                target_include_directories(${pkg_name} INTERFACE ${${pkg_name}_INC})
332            endif()
333
334        elseif(PKG_ONLY_MAKE)
335            __exec_cmd(COMMAND ${CMAKE_MAKE_PROGRAM} ${${pkg_name}_CXXFLAGS} -j${THNUM}
336                    WORKING_DIRECTORY ${${pkg_name}_SOURCE_DIR})
337            set(PKG_INSTALL_INCS ${PKG_ONLY_MAKE_INCS})
338            set(PKG_INSTALL_LIBS ${PKG_ONLY_MAKE_LIBS})
339            file(GLOB ${pkg_name}_INSTALL_INCS ${${pkg_name}_SOURCE_DIR}/${PKG_INSTALL_INCS})
340            file(GLOB ${pkg_name}_INSTALL_LIBS ${${pkg_name}_SOURCE_DIR}/${PKG_INSTALL_LIBS})
341            file(COPY ${${pkg_name}_INSTALL_INCS} DESTINATION ${${pkg_name}_BASE_DIR}/include)
342            file(COPY ${${pkg_name}_INSTALL_LIBS} DESTINATION ${${pkg_name}_BASE_DIR}/lib)
343
344        elseif(PKG_CMAKE_OPTION)
345            # in cmake
346            file(MAKE_DIRECTORY ${${pkg_name}_SOURCE_DIR}/_build)
347            if(${pkg_name}_CFLAGS)
348                set(${pkg_name}_CMAKE_CFLAGS "-DCMAKE_C_FLAGS=${${pkg_name}_CFLAGS}")
349            endif()
350            if(${pkg_name}_CXXFLAGS)
351                set(${pkg_name}_CMAKE_CXXFLAGS "-DCMAKE_CXX_FLAGS=${${pkg_name}_CXXFLAGS}")
352            endif()
353
354            if(${pkg_name}_LDFLAGS)
355                if(${pkg_name}_USE_STATIC_LIBS)
356                    #set(${pkg_name}_CMAKE_LDFLAGS "-DCMAKE_STATIC_LINKER_FLAGS=${${pkg_name}_LDFLAGS}")
357                else()
358                    set(${pkg_name}_CMAKE_LDFLAGS "-DCMAKE_SHARED_LINKER_FLAGS=${${pkg_name}_LDFLAGS}")
359                endif()
360            endif()
361            if(APPLE)
362                __exec_cmd(COMMAND ${CMAKE_COMMAND} ${PKG_CMAKE_OPTION}
363                        ${${pkg_name}_CMAKE_CFLAGS} ${${pkg_name}_CMAKE_CXXFLAGS} ${${pkg_name}_CMAKE_LDFLAGS}
364                        -DCMAKE_INSTALL_PREFIX=${${pkg_name}_BASE_DIR} ${${pkg_name}_SOURCE_DIR}/${PKG_CMAKE_PATH}
365                        WORKING_DIRECTORY ${${pkg_name}_SOURCE_DIR}/_build)
366                __exec_cmd(COMMAND ${CMAKE_COMMAND} --build . --target install --
367                        WORKING_DIRECTORY ${${pkg_name}_SOURCE_DIR}/_build)
368            else()
369                __exec_cmd(COMMAND ${CMAKE_COMMAND} ${PKG_CMAKE_OPTION} -G ${CMAKE_GENERATOR}
370                    ${${pkg_name}_CMAKE_CFLAGS} ${${pkg_name}_CMAKE_CXXFLAGS} ${${pkg_name}_CMAKE_LDFLAGS}
371                    -DCMAKE_INSTALL_PREFIX=${${pkg_name}_BASE_DIR} ${${pkg_name}_SOURCE_DIR}/${PKG_CMAKE_PATH}
372                    WORKING_DIRECTORY ${${pkg_name}_SOURCE_DIR}/_build)
373                __exec_cmd(COMMAND ${CMAKE_COMMAND} --build . --target install -- -j${THNUM}
374                        WORKING_DIRECTORY ${${pkg_name}_SOURCE_DIR}/_build)
375            endif()
376        else()
377            if(${pkg_name}_CFLAGS)
378                set(${pkg_name}_MAKE_CFLAGS "CFLAGS=${${pkg_name}_CFLAGS}")
379            endif()
380            if(${pkg_name}_CXXFLAGS)
381                set(${pkg_name}_MAKE_CXXFLAGS "CXXFLAGS=${${pkg_name}_CXXFLAGS}")
382            endif()
383            if(${pkg_name}_LDFLAGS)
384                set(${pkg_name}_MAKE_LDFLAGS "LDFLAGS=${${pkg_name}_LDFLAGS}")
385            endif()
386            # in configure && make
387            if(PKG_PRE_CONFIGURE_COMMAND)
388                __exec_cmd(COMMAND ${PKG_PRE_CONFIGURE_COMMAND}
389                        WORKING_DIRECTORY ${${pkg_name}_SOURCE_DIR})
390            endif()
391
392            if(PKG_CONFIGURE_COMMAND)
393                __exec_cmd(COMMAND ${PKG_CONFIGURE_COMMAND}
394                        ${${pkg_name}_MAKE_CFLAGS} ${${pkg_name}_MAKE_CXXFLAGS} ${${pkg_name}_MAKE_LDFLAGS}
395                        --prefix=${${pkg_name}_BASE_DIR}
396                        WORKING_DIRECTORY ${${pkg_name}_SOURCE_DIR})
397            endif()
398            set(${pkg_name}_BUILD_OPTION ${PKG_BUILD_OPTION})
399            if(NOT PKG_CONFIGURE_COMMAND)
400                set(${pkg_name}_BUILD_OPTION ${${pkg_name}_BUILD_OPTION}
401                        ${${pkg_name}_MAKE_CFLAGS} ${${pkg_name}_MAKE_CXXFLAGS} ${${pkg_name}_MAKE_LDFLAGS})
402            endif()
403            # build
404            if(APPLE)
405                __exec_cmd(COMMAND ${CMAKE_MAKE_PROGRAM} ${${pkg_name}_BUILD_OPTION}
406                        WORKING_DIRECTORY ${${pkg_name}_SOURCE_DIR})
407            else()
408                __exec_cmd(COMMAND ${CMAKE_MAKE_PROGRAM} ${${pkg_name}_BUILD_OPTION} -j${THNUM}
409                        WORKING_DIRECTORY ${${pkg_name}_SOURCE_DIR})
410            endif()
411
412            if(PKG_INSTALL_INCS OR PKG_INSTALL_LIBS)
413                file(GLOB ${pkg_name}_INSTALL_INCS ${${pkg_name}_SOURCE_DIR}/${PKG_INSTALL_INCS})
414                file(GLOB ${pkg_name}_INSTALL_LIBS ${${pkg_name}_SOURCE_DIR}/${PKG_INSTALL_LIBS})
415                file(COPY ${${pkg_name}_INSTALL_INCS} DESTINATION ${${pkg_name}_BASE_DIR}/include)
416                file(COPY ${${pkg_name}_INSTALL_LIBS} DESTINATION ${${pkg_name}_BASE_DIR}/lib)
417            else()
418                __exec_cmd(COMMAND ${CMAKE_MAKE_PROGRAM} install WORKING_DIRECTORY ${${pkg_name}_SOURCE_DIR})
419            endif()
420        endif()
421    endif()
422
423    if(PKG_LIBS)
424        __find_pkg_then_add_target(${pkg_name} ${PKG_EXE} ${PKG_LIB_PATH} ${PKG_LIBS})
425        set(${pkg_name}_INC ${${pkg_name}_BASE_DIR}/include PARENT_SCOPE)
426        if(NOT ${pkg_name}_LIBS)
427            message(FATAL_ERROR "Can not find pkg: ${pkg_name}")
428        endif()
429    else()
430        find_package(${__FIND_PKG_NAME} ${PKG_VER} QUIET ${MS_FIND_NO_DEFAULT_PATH})
431        if(${__FIND_PKG_NAME}_FOUND)
432            set(${pkg_name}_INC ${${pkg_name}_BASE_DIR}/include PARENT_SCOPE)
433            message("Found pkg: ${${__FIND_PKG_NAME}_LIBRARIES}")
434            return()
435        endif()
436    endif()
437endfunction()
438