• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1if(MSVC)
2    set(flatbuffers_CXXFLAGS "${CMAKE_CXX_FLAGS}")
3    set(flatbuffers_CFLAGS "${CMAKE_C_FLAGS}")
4    set(flatbuffers_LDFLAGS "${CMAKE_SHARED_LINKER_FLAGS}")
5else()
6    set(flatbuffers_CXXFLAGS "-fPIC -fPIE -D_FORTIFY_SOURCE=2 -O2 -fstack-protector-strong")
7    set(flatbuffers_CFLAGS "-fPIC -fPIE -D_FORTIFY_SOURCE=2 -O2 -fstack-protector-strong")
8endif()
9
10if(WIN32)
11    set(flatbuffers_USE_STATIC_LIBS ON)
12endif()
13
14if(ENABLE_GITEE)
15    set(REQ_URL "https://gitee.com/mirrors/flatbuffers/repository/archive/v2.0.0.tar.gz")
16    set(MD5 "4181afaa9c279bd6c690a36bc15a23d2")
17else()
18    set(REQ_URL "https://github.com/google/flatbuffers/archive/v2.0.0.tar.gz")
19    set(MD5 "a27992324c3cbf86dd888268a23d17bd")
20endif()
21
22if(APPLE)
23    mindspore_add_pkg(flatbuffers
24            VER 2.0.0
25            LIBS flatbuffers
26            EXE flatc
27            URL ${REQ_URL}
28            MD5 ${MD5}
29            CMAKE_OPTION -DFLATBUFFERS_BUILD_TESTS=OFF -DCMAKE_INSTALL_LIBDIR=lib)
30else()
31    mindspore_add_pkg(flatbuffers
32            VER 2.0.0
33            LIBS flatbuffers
34            EXE flatc
35            URL ${REQ_URL}
36            MD5 ${MD5}
37            CMAKE_OPTION -DCMAKE_C_COMPILER=${FLATC_GCC_COMPILER} -DCMAKE_CXX_COMPILER=${FLATC_GXX_COMPILER}
38            -DFLATBUFFERS_BUILD_TESTS=OFF -DCMAKE_INSTALL_LIBDIR=lib -DCMAKE_BUILD_TYPE=Release)
39endif()
40
41include_directories(${flatbuffers_INC})
42add_library(mindspore::flatbuffers ALIAS flatbuffers::flatbuffers)
43add_executable(mindspore::flatc ALIAS flatbuffers::flatc)
44function(ms_build_flatbuffers source_schema_files
45                              source_schema_dirs
46                              custom_target_name
47                              generated_output_dir)
48
49    set(total_schema_dirs "")
50    set(total_generated_files "")
51    set(FLATC mindspore::flatc)
52    foreach(schema_dir ${source_schema_dirs})
53        set(total_schema_dirs -I ${schema_dir} ${total_schema_dirs})
54    endforeach()
55
56    foreach(schema IN LISTS ${source_schema_files})
57        get_filename_component(filename ${schema} NAME_WE)
58        if(NOT ${generated_output_dir} STREQUAL "")
59            set(generated_file ${generated_output_dir}/${filename}_generated.h)
60            add_custom_command(
61                    OUTPUT ${generated_file}
62                    COMMAND ${FLATC} --gen-mutable
63                    --reflect-names --gen-object-api -o ${generated_output_dir}
64                    ${total_schema_dirs}
65                    -c -b --reflect-types ${schema}
66                    DEPENDS ${FLATC} ${schema}
67                    WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
68                    COMMENT "Running C++ flatbuffers compiler on ${schema}" VERBATIM)
69            list(APPEND total_generated_files ${generated_file})
70        endif()
71    endforeach()
72
73    add_custom_target(${custom_target_name} ALL
74            DEPENDS ${total_generated_files})
75
76    if(NOT ${generated_output_dir} STREQUAL "")
77        include_directories(${generated_output_dir})
78        set_property(TARGET ${custom_target_name}
79                PROPERTY GENERATED_OUTPUT_DIR
80                ${generated_output_dir})
81    endif()
82endfunction()
83
84function(ms_build_flatbuffers_lite
85  source_schema_files source_schema_dirs custom_target_name generated_output_dir if_inner)
86
87    set(total_schema_dirs "")
88    set(total_generated_files "")
89    set(FLATC mindspore::flatc)
90    foreach(schema_dir ${source_schema_dirs})
91        set(total_schema_dirs -I ${schema_dir} ${total_schema_dirs})
92    endforeach()
93
94    foreach(schema IN LISTS ${source_schema_files})
95        get_filename_component(filename ${schema} NAME_WE)
96        if(NOT ${generated_output_dir} STREQUAL "")
97            set(generated_file ${generated_output_dir}/${filename}_generated.h)
98            if(if_inner MATCHES "inner")
99                add_custom_command(
100                        OUTPUT ${generated_file}
101                        COMMAND ${FLATC} --gen-mutable
102                        --reflect-names --gen-object-api -o ${generated_output_dir}
103                        ${total_schema_dirs}
104                        -c -b --reflect-types ${schema}
105                        DEPENDS ${FLATC} ${schema}
106                        WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
107                        COMMENT "Running C++ flatbuffers compiler on ${schema}" VERBATIM)
108            else()
109                add_custom_command(
110                        OUTPUT ${generated_file}
111                        COMMAND ${FLATC} -o ${generated_output_dir}
112                        ${total_schema_dirs}
113                        -c -b  ${schema}
114                        DEPENDS ${FLATC} ${schema}
115                        WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
116                        COMMENT "Running C++ flatbuffers compiler on ${schema}" VERBATIM)
117            endif()
118            list(APPEND total_generated_files ${generated_file})
119        endif()
120    endforeach()
121
122    add_custom_target(${custom_target_name} ALL
123            DEPENDS ${total_generated_files})
124
125    if(NOT ${generated_output_dir} STREQUAL "")
126        include_directories(${generated_output_dir})
127        set_property(TARGET ${custom_target_name}
128                PROPERTY GENERATED_OUTPUT_DIR
129                ${generated_output_dir})
130    endif()
131endfunction()
132