1cmake_minimum_required (VERSION 2.6)
2
3project (miniupnpc C)
4set (MINIUPNPC_VERSION 1.9)
5set (MINIUPNPC_API_VERSION 12)
6
7if (NOT CMAKE_BUILD_TYPE)
8  if (WIN32)
9    set (DEFAULT_BUILD_TYPE MinSizeRel)
10  else (WIN32)
11    set (DEFAULT_BUILD_TYPE RelWithDebInfo)
12  endif(WIN32)
13    set (CMAKE_BUILD_TYPE ${DEFAULT_BUILD_TYPE} CACHE STRING
14        "Choose the type of build, options are: Debug Release RelWithDebInfo MinSizeRel."
15        FORCE)
16endif()
17
18option (UPNPC_BUILD_STATIC "Build static library" TRUE)
19option (UPNPC_BUILD_SHARED "Build shared library" TRUE)
20if (NOT WIN32)
21  option (UPNPC_BUILD_TESTS "Build test executables" TRUE)
22endif (NOT WIN32)
23option (NO_GETADDRINFO "Define NO_GETADDRINFO" FALSE)
24
25mark_as_advanced (NO_GETADDRINFO)
26
27if (NO_GETADDRINFO)
28  add_definitions (-DNO_GETADDRINFO)
29endif (NO_GETADDRINFO)
30
31if (NOT WIN32)
32  add_definitions (-DMINIUPNPC_SET_SOCKET_TIMEOUT)
33  add_definitions (-D_BSD_SOURCE -D_POSIX_C_SOURCE=200112L)
34else (NOT WIN32)
35  add_definitions (-D_WIN32_WINNT=0x0501) # XP or higher for getnameinfo and friends
36endif (NOT WIN32)
37
38if (CMAKE_SYSTEM_NAME STREQUAL "Darwin")
39  add_definitions (-D_DARWIN_C_SOURCE)
40endif ()
41
42# Set compiler specific build flags
43if (CMAKE_COMPILER_IS_GNUC)
44  # Set our own default flags at first run.
45  if (NOT CONFIGURED)
46
47    if (NOT CMAKE_SYSTEM_NAME STREQUAL "AmigaOS")
48      set (_PIC -fPIC)
49    endif (CMAKE_SYSTEM_NAME STREQUAL "AmigaOS")
50
51    set (CMAKE_C_FLAGS "${_PIC} -Wall $ENV{CFLAGS}" # CMAKE_C_FLAGS gets appended to the other C flags
52        CACHE STRING "Flags used by the C compiler during normal builds." FORCE)
53    set (CMAKE_C_FLAGS_DEBUG "-g -DDDEBUG"
54        CACHE STRING "Flags used by the C compiler during debug builds." FORCE)
55    set (CMAKE_C_FLAGS_RELEASE "-O2 -DNDEBUG"
56        CACHE STRING "Flags used by the C compiler during release builds." FORCE)
57    set (CMAKE_C_FLAGS_RELWITHDEBINFO "-O2 -g -DNDEBUG"
58        CACHE STRING "Flags used by the C compiler during release builds." FORCE)
59    set (CMAKE_C_FLAGS_MINSIZEREL "-Os -DNDEBUG"
60        CACHE STRING "Flags used by the C compiler during release builds." FORCE)
61
62  endif (NOT CONFIGURED)
63endif ()
64
65configure_file (${CMAKE_SOURCE_DIR}/miniupnpcstrings.h.cmake ${CMAKE_BINARY_DIR}/miniupnpcstrings.h)
66include_directories (${CMAKE_BINARY_DIR})
67
68set (MINIUPNPC_SOURCES
69  igd_desc_parse.c
70  miniupnpc.c
71  minixml.c
72  minisoap.c
73  miniwget.c
74  upnpc.c
75  upnpcommands.c
76  upnpreplyparse.c
77  upnperrors.c
78  connecthostport.c
79  portlistingparse.c
80  receivedata.c
81)
82
83if (NOT WIN32 AND NOT CMAKE_SYSTEM_NAME STREQUAL "AmigaOS")
84  set (MINIUPNPC_SOURCES ${MINIUPNPC_SOURCES} minissdpc.c)
85endif (NOT WIN32 AND NOT CMAKE_SYSTEM_NAME STREQUAL "AmigaOS")
86
87if (WIN32)
88  set_source_files_properties (${MINIUPNPC_SOURCES} PROPERTIES
89                                                    COMPILE_DEFINITIONS MINIUPNP_STATICLIB
90                                                    COMPILE_DEFINITIONS MINIUPNP_EXPORTS
91  )
92endif (WIN32)
93
94if (WIN32)
95  find_library (WINSOCK2_LIBRARY NAMES ws2_32 WS2_32 Ws2_32)
96  find_library (IPHLPAPI_LIBRARY NAMES iphlpapi)
97  set (LDLIBS ${WINSOCK2_LIBRARY} ${IPHLPAPI_LIBRARY} ${LDLIBS})
98#elseif (CMAKE_SYSTEM_NAME STREQUAL "Solaris")
99#  find_library (SOCKET_LIBRARY NAMES socket)
100#  find_library (NSL_LIBRARY NAMES nsl)
101#  find_library (RESOLV_LIBRARY NAMES resolv)
102#  set (LDLIBS ${SOCKET_LIBRARY} ${NSL_LIBRARY} ${RESOLV_LIBRARY} ${LDLIBS})
103endif (WIN32)
104
105if (NOT UPNPC_BUILD_STATIC AND NOT UPNPC_BUILD_SHARED)
106    message (FATAL "Both shared and static libraries are disabled!")
107endif (NOT UPNPC_BUILD_STATIC AND NOT UPNPC_BUILD_SHARED)
108
109if (UPNPC_BUILD_STATIC)
110  add_library (upnpc-static STATIC ${MINIUPNPC_SOURCES})
111  set_target_properties (upnpc-static PROPERTIES OUTPUT_NAME "miniupnpc")
112  target_link_libraries (upnpc-static ${LDLIBS})
113  set (UPNPC_INSTALL_TARGETS ${UPNPC_INSTALL_TARGETS} upnpc-static)
114  set (UPNPC_LIBRARY_TARGET upnpc-static)
115endif (UPNPC_BUILD_STATIC)
116
117if (UPNPC_BUILD_SHARED)
118  add_library (upnpc-shared SHARED ${MINIUPNPC_SOURCES})
119  set_target_properties (upnpc-shared PROPERTIES OUTPUT_NAME "miniupnpc")
120  set_target_properties (upnpc-shared PROPERTIES VERSION ${MINIUPNPC_VERSION})
121  set_target_properties (upnpc-shared PROPERTIES SOVERSION ${MINIUPNPC_API_VERSION})
122  target_link_libraries (upnpc-shared ${LDLIBS})
123  set (UPNPC_INSTALL_TARGETS ${UPNPC_INSTALL_TARGETS} upnpc-shared)
124  set (UPNPC_LIBRARY_TARGET upnpc-shared)
125endif (UPNPC_BUILD_SHARED)
126
127if (UPNPC_BUILD_TESTS)
128  add_executable (testminixml testminixml.c minixml.c igd_desc_parse.c)
129  target_link_libraries (testminixml ${LDLIBS})
130
131  add_executable (minixmlvalid minixmlvalid.c minixml.c)
132  target_link_libraries (minixmlvalid ${LDLIBS})
133
134  add_executable (testupnpreplyparse testupnpreplyparse.c
135                                     minixml.c upnpreplyparse.c)
136  target_link_libraries (testupnpreplyparse ${LDLIBS})
137
138  add_executable (testigddescparse testigddescparse.c
139                                   igd_desc_parse.c minixml.c miniupnpc.c miniwget.c minissdpc.c
140                                   upnpcommands.c upnpreplyparse.c minisoap.c connecthostport.c
141                                   portlistingparse.c receivedata.c
142  )
143  target_link_libraries (testigddescparse ${LDLIBS})
144
145  add_executable (testminiwget testminiwget.c
146                               miniwget.c miniupnpc.c minisoap.c upnpcommands.c minissdpc.c
147                               upnpreplyparse.c minixml.c igd_desc_parse.c connecthostport.c
148                               portlistingparse.c receivedata.c
149  )
150  target_link_libraries (testminiwget ${LDLIBS})
151
152# set (UPNPC_INSTALL_TARGETS ${UPNPC_INSTALL_TARGETS} testminixml minixmlvalid testupnpreplyparse testigddescparse testminiwget)
153endif (UPNPC_BUILD_TESTS)
154
155
156install (TARGETS ${UPNPC_INSTALL_TARGETS}
157  RUNTIME DESTINATION bin
158  LIBRARY DESTINATION lib${LIB_SUFFIX}
159  ARCHIVE DESTINATION lib${LIB_SUFFIX}
160)
161install (FILES
162	miniupnpc.h
163  miniwget.h
164  upnpcommands.h
165  igd_desc_parse.h
166  upnpreplyparse.h
167  upnperrors.h
168  miniupnpctypes.h
169  portlistingparse.h
170  declspec.h
171  DESTINATION include/miniupnpc
172)
173
174set (CONFIGURED YES CACHE INTERNAL "")
175
176# vim: ts=2:sw=2
177