Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cmake build script for the amalgamated version #10

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

jingyu
Copy link

@jingyu jingyu commented May 7, 2023

Support build SQLite3 amalgamated version with CMake.
All build options are same with the configure interface.

Tested platforms:

  • Linux
  • MacOS
  • Windows

CMake scripts provided a easy and unified build method for the cross-platform projects that using SQLite library.

@justinclift
Copy link

Shouldn't this be proposed on the SQLite Forum first? 😄

@jingyu
Copy link
Author

jingyu commented May 8, 2023

Shouldn't this be proposed on the SQLite Forum first? 😄

Sorry, I thought it would be enough to just submit a PR request here.

@justinclift
Copy link

That's a good thought, it's just the SQLite project doesn't really use GitHub much apart from being a mirror here. You're much better off jumping on their Forum and discussing stuff with them there, first. 😄

@nono303
Copy link

nono303 commented May 8, 2023

Many thx @jingyu for this initiative!
There was already a cmake fork for https://github.com/rhuijben/sqlite-amalgamation initiated by @rhuijben to which I was able to contribute.
I merged the files to keep the best of both (+ correction of a typo)
Here is the diff result.
I let you see with the "forum approval process" and if it seemed relevant to you, I’ll change it in the PR.

diff --git "a/S:\\telecharger\\httpsgithub.comsqlitesqlitepull10_CMakeLists.txt" "b/C:\\sdk\\src\\sqlite\\CMakeLists.txt"
index ea7da61..a2351f0 100644
--- "a/S:\\telecharger\\httpsgithub.comsqlitesqlitepull10_CMakeLists.txt"
+++ "b/C:\\sdk\\src\\sqlite\\CMakeLists.txt"
@@ -1,24 +1,61 @@
 cmake_minimum_required(VERSION 3.18)
-project(sqlite3 VERSION 3.42.0 HOMEPAGE_URL "http://www.sqlite.org")
+
+project(sqlite3
+    VERSION   3.41.2
+    LANGUAGES C
+    HOMEPAGE_URL "http://www.sqlite.org"
+)
 
 include(CheckIncludeFile)
 include(CheckSymbolExists)
 include(CheckFunctionExists)
+include(GNUInstallDirs)
+
+# build options and optional modules (https://www.sqlite.org/compile.html):
+
+option(ENABLE_SHARED             "Build shared library"                           ON)
+option(SHAREDLIB_PREFIX          "Prefix name for shared lib"                     "")
+option(ENABLE_STATIC             "Build static library"                           OFF)
+option(BUILD_SHELL               "Build SQLite3 shell application"                ON)
+option(ENABLE_STATIC_SHELL       "Statically link shell tool"                     ON)
+option(ENABLE_DEBUG              "Build with debugging features enabled"          OFF)
+option(ENABLE_EDITLINE           "Use BSD libedit"                                ON)
+option(ENABLE_READLINE           "Use GNU readline"                               ON)
+option(ENABLE_ICU                "Enable international components for unicode"    OFF)
+option(ENABLE_THREADSAFE         "Build a thread-safe library"                    ON)
+option(ENABLE_DYNAMIC_EXTENSIONS "Support loadable extensions"                    ON)
+option(ENABLE_MATH               "SQL math functions"                             ON)
+option(ENABLE_FTS4               "Include fts4 support"                           ON)
+option(ENABLE_FTS3               "Include fts3 support"                           OFF)
+option(ENABLE_FTS5               "Include fts5 support"                           ON)
+option(ENABLE_RTREE              "Include rtree support"                          ON)
+option(ENABLE_SESSION            "Enable the session extension"                   OFF)
+option(ENABLE_COLUMN_METADATA    "Enable column metadata"                         OFF)
+option(ENABLE_DBSTAT_VTAB        "Enable dbstat virtual table"                    OFF)
+option(ENABLE_RBU                "Enable resumable bulk update extension"         OFF)
+option(ENABLE_STAT4              "Enhance query planner under certain situations" OFF)
+option(OMIT_DECLTYPE             "Omit declared type of columns"                  ON)
+option(OMIT_JSON                 "Omit JSON SQL functions"                        OFF)
+option(OMIT_AUTOINIT             "Omit automatic initialization"                  OFF)
+option(RECOMMENDED_OPTIONS       "Compile by SQLite3 recommended options"         ON)
+option(USE_URI                   "Enable the default URI filename processing"     OFF)
+if(WIN32)
+    option(WIN32_MALLOC              "Use Windows Heap API functions for memory allocation" OFF)
+    option(WIN32_HEAP_CREATE         "Force the Win32 native memory allocator"              OFF)
+    if(${CMAKE_SIZEOF_VOID_P} LESS 8)
+        option(BUILD_WITH_XPSDK          "Build for old 32bit (WinXP/2003) targets"         OFF)
+    endif()
+endif()
 
-set(ENABLE_SHARED ON CACHE BOOL "Build shared library")
-set(ENABLE_STATIC ON CACHE BOOL "Build static library")
-set(ENABLE_EDITLINE ON CACHE BOOL "Use BSD libedit")
-set(ENABLE_READLINE ON CACHE BOOL "Use GNU readline")
-set(ENABLE_THREADSAFE ON CACHE BOOL "Build a thread-safe library")
-set(ENABLE_DYNAMIC_EXTENSIONS ON CACHE BOOL "Support loadable extensions")
-set(ENABLE_MATH ON CACHE BOOL "SQL math functions")
-set(ENABLE_FTS4 ON CACHE BOOL "Include fts4 support")
-set(ENABLE_FTS3 OFF CACHE BOOL "Include fts3 support")
-set(ENABLE_FTS5 ON CACHE BOOL "Include fts5 support")
-set(ENABLE_RTREE ON CACHE BOOL "Include rtree support")
-set(ENABLE_SESSION OFF CACHE BOOL "Enable the session extension")
-set(ENABLE_DEBUG OFF CACHE BOOL "Build with debugging features enabled")
-set(ENABLE_STATIC_SHELL ON CACHE BOOL "Statically link shell tool")
+if(NOT CMAKE_BUILD_TYPE)
+    set(CMAKE_BUILD_TYPE "Release" CACHE STRING "Release or Debug?" FORCE)
+endif()
+
+if(SQLITE_ENABLE_COLUMN_METADATA AND SQLITE_OMIT_DECLTYPE) # https://github.com/sqlite/sqlite/blob/master/src/vdbeapi.c#L1355
+    message(FATAL_ERROR "please unset the SQLITE_OMIT_DECLTYPE if you want to\
+    compile with SQLITE_ENABLE_COLUMN_METADATA,\
+    compiling with both options ON, is not recommended.")
+endif()
 
 check_include_file(stdio.h HAVE_STDIO_H)
 if(HAVE_STDIO_H)
@@ -214,14 +251,20 @@ endif()
 check_include_file(zlib.h HAVE_ZLIB_H)
 if(HAVE_ZLIB_H)
     add_definitions(-DHAVE_ZLIB_H=1)
-
-    set(CMAKE_REQUIRED_LIBRARIES z)
-    check_function_exists(deflate SQLITE_HAVE_ZLIB)
-    unset(CMAKE_REQUIRED_LIBRARIES)
-
-    if(SQLITE_HAVE_ZLIB)
+    find_package(ZLIB)
+    if(ZLIB_FOUND)
         add_definitions(-DSQLITE_HAVE_ZLIB=1)
-        set(ZLIB_LIBRARIES z)
+    set(HAVE_ZLIB ON CACHE BOOL "Use zlib" FORCE)
+    else()
+    set(HAVE_ZLIB OFF CACHE BOOL "Use zlib" FORCE)
+    endif()
+endif()
+
+# Check icu
+if(ENABLE_ICU)
+    find_package(ICU COMPONENTS uc i18n)
+    if(ICU_FOUND)
+        add_definitions(-DSQLITE_ENABLE_ICU=1)
     endif()
 endif()
 
@@ -278,6 +321,69 @@ if(ENABLE_SESSION)
     add_definitions(-DSQLITE_ENABLE_SESSION=1 -DSQLITE_ENABLE_PREUPDATE_HOOK=1)
 endif()
 
+if(ENABLE_COLUMN_METADATA)
+    add_definitions(-DSQLITE_ENABLE_COLUMN_METADATA=1)
+endif()
+
+if(ENABLE_DBSTAT_VTAB)
+    add_definitions(-DSQLITE_ENABLE_DBSTAT_VTAB=1)
+endif()
+
+if(ENABLE_RBU)
+    add_definitions(-DSQLITE_ENABLE_RBU=1)
+endif()
+
+if(ENABLE_STAT4)
+    add_definitions(-DSQLITE_ENABLE_STAT4=1)
+endif()
+
+if(OMIT_DECLTYPE)
+    add_definitions(-DSQLITE_OMIT_DECLTYPE=1)
+endif()
+
+if(OMIT_JSON)
+    add_definitions(-DSQLITE_OMIT_JSON=1)
+endif()
+
+if(OMIT_AUTOINIT)
+    add_definitions(-DSQLITE_OMIT_AUTOINIT=1)
+endif()
+
+if(USE_URI)
+    add_definitions(-DSQLITE_USE_URI=1)
+endif()
+
+if(WIN32_MALLOC)
+    if(WIN32)
+        add_definitions(-DSQLITE_WIN32_MALLOC=1)
+    endif()
+endif()
+
+if(WIN32_HEAP_CREATE)
+    if(WIN32)
+        add_definitions(-DSQLITE_WIN32_HEAP_CREATE=1)
+    endif()
+endif()
+
+if(BUILD_WITH_XPSDK)
+    add_definitions(-DSQLITE_OS_WINRT=0 
+    -D_WIN32_WINNT=0x0502 
+    -DWINVER=0x0502)
+endif()
+
+# https://www.sqlite.org/compile.html#recommended_compile_time_options
+if(RECOMMENDED_OPTIONS)
+    add_definitions(-DSQLITE_DEFAULT_WAL_SYNCHRONOUS=1
+        -DSQLITE_DQS=0
+        -DSQLITE_LIKE_DOESNT_MATCH_BLOBS
+        -DSQLITE_MAX_EXPR_DEPTH=0
+        -DSQLITE_OMIT_DEPRECATED
+        -DSQLITE_OMIT_PROGRESS_CALLBACK
+        -DSQLITE_OMIT_SHARED_CACHE
+        -DSQLITE_USE_ALLOCA
+    -DSQLITE_DEFAULT_MEMSTATUS=0)
+endif()        
+
 if(ENABLE_DEBUG)
     add_definitions(-DSQLITE_DEBUG=1
         -DSQLITE_ENABLE_SELECTTRACE=1
@@ -287,15 +393,12 @@ endif()
 add_definitions(
     -DSQLITE_ENABLE_EXPLAIN_COMMENTS=1
     -DSQLITE_ENABLE_DBPAGE_VTAB=1
-    -DSQLITE_ENABLE_STMTVTAB=1
-    -DSQLITE_ENABLE_DBSTAT_VTAB=1)
+    -DSQLITE_ENABLE_STMTVTAB=1)
 
 if(WIN32)
     add_definitions(
-        -DSQLITE_ENABLE_RBU=1
         -DSQLITE_ENABLE_OFFSET_SQL_FUNC=1
         -DSQLITE_ENABLE_BYTECODE_VTAB=1
-        -DSQLITE_ENABLE_COLUMN_METADATA=1
         -DSQLITE_WIN32_USE_UUID=1)
 
     set(UUID_LIBRARIES Rpcrt4.lib)
@@ -313,7 +416,7 @@ if(HAVE_SQLITE_CONFIG_H)
 endif()
 
 if(ENABLE_STATIC_SHELL)
-    if(NOT EANBLED_STATIC)
+    if(NOT ENABLE_STATIC)
         set(ENABLE_STATIC ON CACHE BOOL "Build static library" FORCE)
     endif()
 else()
@@ -322,20 +425,36 @@ else()
     endif()
 endif()
 
-message(STATUS "Build shared library... ${ENABLE_SHARED}")
-message(STATUS "Build static library... ${ENABLE_STATIC}")
-message(STATUS "Use BSD libedit... ${ENABLE_EDITLINE}")
-message(STATUS "Use GNU readline... ${ENABLE_READLINE}")
-message(STATUS "Build a thread-safe library... ${ENABLE_THREADSAFE}")
-message(STATUS "Support loadable extensions... ${ENABLE_DYNAMIC_EXTENSIONS}")
-message(STATUS "SQL math functions... ${ENABLE_MATH}")
-message(STATUS "Include fts4 support... ${ENABLE_FTS4}")
-message(STATUS "Include fts3 support... ${ENABLE_FTS3}")
-message(STATUS "Include fts5 support... ${ENABLE_FTS5}")
-message(STATUS "Include rtree support... ${ENABLE_RTREE}")
-message(STATUS "Enable the session extension... ${ENABLE_SESSION}")
-message(STATUS "Build with debugging features enabled... ${ENABLE_DEBUG}")
-message(STATUS "Statically link shell tool... ${ENABLE_STATIC_SHELL}")
+message(STATUS "Build shared library:                                 ${ENABLE_SHARED}")
+message(STATUS "Shared library name:                                  ${SHAREDLIB_PREFIX}${PROJECT_NAME}")
+message(STATUS "Build static library:                                 ${ENABLE_STATIC}")
+message(STATUS "Build shell tool:                                     ${BUILD_SHELL}")
+message(STATUS "Statically link shell tool:                           ${ENABLE_STATIC_SHELL}")
+message(STATUS "Build with debugging features enabled:                ${ENABLE_DEBUG}")
+message(STATUS "Have zlib:                                            ${ZLIB_FOUND}")
+message(STATUS "Use BSD libedit:                                      ${ENABLE_EDITLINE}")
+message(STATUS "Use GNU readline:                                     ${ENABLE_READLINE}")
+message(STATUS "Use international components for unicode:             ${ENABLE_ICU}")
+message(STATUS "Build a thread-safe library:                          ${ENABLE_THREADSAFE}")
+message(STATUS "Support loadable extensions:                          ${ENABLE_DYNAMIC_EXTENSIONS}")
+message(STATUS "SQL math functions:                                   ${ENABLE_MATH}")
+message(STATUS "Include fts4 support:                                 ${ENABLE_FTS4}")
+message(STATUS "Include fts3 support:                                 ${ENABLE_FTS3}")
+message(STATUS "Include fts5 support:                                 ${ENABLE_FTS5}")
+message(STATUS "Include rtree support:                                ${ENABLE_RTREE}")
+message(STATUS "Enable the session extension:                         ${ENABLE_SESSION}")
+message(STATUS "Enable column metadata:                               ${ENABLE_COLUMN_METADATA}")
+message(STATUS "Enable dbstat virtual table:                          ${ENABLE_DBSTAT_VTAB}")
+message(STATUS "Enable resumable bulk update extension:               ${ENABLE_RBU}")
+message(STATUS "Enhance query planner under certain situations:       ${ENABLE_STAT4}")
+message(STATUS "Omit declared type of columns:                        ${OMIT_DECLTYPE}")
+message(STATUS "Omit JSON SQL functions:                              ${OMIT_JSON}")
+message(STATUS "Omit automatic initialization:                        ${OMIT_AUTOINIT}")
+message(STATUS "Compile by SQLite3 recommended options:               ${RECOMMENDED_OPTIONS}")
+message(STATUS "Enable the default URI filename processing:           ${USE_URI}")
+message(STATUS "Use Windows Heap API functions for memory allocation: ${WIN32_MALLOC}")
+message(STATUS "Force the Win32 native memory allocator:              ${WIN32_HEAP_CREATE}")
+message(STATUS "Build for old 32bit (WinXP/2003) targets:             ${BUILD_WITH_XPSDK}")
 
 set(SQLITE3_LIB_SRC sqlite3.c)
 set(SQLITE3_SHELL_SRC shell.c)
@@ -346,7 +465,8 @@ set(SQLITE3_DEP_LIBRARIES
     ${ZLIB_LIBRARIES}
     ${PTHREAD_LIBRARIES}
     ${DL_LIBRARIES}
-    ${MATH_LIBRARIES})
+    ${MATH_LIBRARIES}
+    ${ICU_LIBRARIES})
 
 # SQLite3 pkg-config file
 if(NOT WIN32)
@@ -368,15 +488,17 @@ if(ENABLE_SHARED)
     add_library(sqlite3-shared SHARED ${SQLITE3_LIB_SRC})
     target_compile_definitions(sqlite3-shared PRIVATE ${SQLITE_API_DECL})
     target_link_libraries(sqlite3-shared ${SQLITE3_DEP_LIBRARIES})
+    target_include_directories(sqlite3-shared PUBLIC ${CMAKE_REQUIRED_INCLUDES})
     set_target_properties(sqlite3-shared PROPERTIES
-        OUTPUT_NAME sqlite3
+        OUTPUT_NAME ${SHAREDLIB_PREFIX}${PROJECT_NAME}
         VERSION ${PROJECT_VERSION}
         SOVERSION ${PROJECT_VERSION_MAJOR})
 
-    install(TARGETS sqlite3-shared
-        RUNTIME DESTINATION "bin"
-        ARCHIVE DESTINATION "lib"
-        LIBRARY DESTINATION "lib")
+    install(TARGETS sqlite3-shared EXPORT ${PROJECT_NAME}Config
+        RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
+        ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
+        LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR})
+    install(FILES ${CMAKE_BINARY_DIR}/${SHAREDLIB_PREFIX_DECORATOR}${PROJECT_NAME}.pdb DESTINATION ${CMAKE_INSTALL_BINDIR} OPTIONAL)
 endif()
 
 if(ENABLE_STATIC)
@@ -386,35 +508,61 @@ if(ENABLE_STATIC)
 
     add_library(sqlite3-static STATIC ${SQLITE3_LIB_SRC})
     set_target_properties(sqlite3-static PROPERTIES
-        OUTPUT_NAME sqlite3${NAME_FIX_DECORATOR})
+        OUTPUT_NAME ${PROJECT_NAME}${NAME_FIX_DECORATOR})
 
-    install(TARGETS sqlite3-static
-        EXPORT SQLite3Config
-        RUNTIME DESTINATION "bin"
-        ARCHIVE DESTINATION "lib"
-        LIBRARY DESTINATION "lib")
+    install(TARGETS sqlite3-static EXPORT ${PROJECT_NAME}Config
+        RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
+        ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
+        LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR})
 endif()
 
-add_executable(sqlite3 ${SQLITE3_SHELL_SRC})
-target_compile_definitions(sqlite3 PRIVATE ${SQLITE3_SHELL_DEFS})
-if(ENABLE_STATIC_SHELL)
-add_dependencies(sqlite3 sqlite3-static)
-    target_link_libraries(sqlite3 sqlite3-static ${SQLITE3_DEP_LIBRARIES})
-else()
-    add_dependencies(sqlite3 sqlite3-shared)
-    target_link_libraries(sqlite3 sqlite3-shared)
+if(BUILD_SHELL)
+    add_executable(sqlite3 ${SQLITE3_SHELL_SRC})
+    target_compile_definitions(sqlite3 PRIVATE ${SQLITE3_SHELL_DEFS})
+    if(ENABLE_STATIC_SHELL)
+        add_dependencies(sqlite3 sqlite3-static)
+        target_link_libraries(sqlite3 sqlite3-static ${SQLITE3_DEP_LIBRARIES})
+        target_include_directories(sqlite3 sqlite3-static PUBLIC ${CMAKE_REQUIRED_INCLUDES})
+    else()
+        add_dependencies(sqlite3 sqlite3-shared)
+        target_link_libraries(sqlite3 sqlite3-shared)
+    endif()
+
+    if(UNIX)
+        if(CMAKE_BUILD_TYPE STREQUAL Release)
+            add_custom_command(TARGET shell_app POST_BUILD
+                COMMAND ${CMAKE_STRIP} ${EXE_NAME}
+            )
+        endif()
+    elseif(MSVC)
+        foreach(flag CMAKE_C_FLAGS_RELEASE CMAKE_C_FLAGS_MINSIZEREL CMAKE_C_FLAGS_DEBUG)
+            if(ENABLE_STATIC_SHELL)
+                string(REGEX REPLACE "/MD" "/MT" ${flag} "${${flag}}")
+            else()
+                string(REGEX REPLACE "/MT" "/MD" ${flag} "${${flag}}")
+            endif()
+            set(${flag} "${${flag}}" CACHE STRING "msvc flags" FORCE)
+        endforeach()
+    endif()
+
+    install(TARGETS sqlite3 EXPORT ${PROJECT_NAME}Config
+        RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
+        ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
+        LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR})
+    install(FILES ${CMAKE_BINARY_DIR}/${PROJECT_NAME}.pdb DESTINATION ${CMAKE_INSTALL_BINDIR} OPTIONAL)
 endif()
 
+configure_file(sqlite3_config.h.in ${CMAKE_BINARY_DIR}/sqlite3_config.h)
+
+install(EXPORT  ${PROJECT_NAME}Config
+    NAMESPACE   SQLite::
+    DESTINATION cmake
+)
+
 install(FILES sqlite3.h sqlite3ext.h
-    DESTINATION "include")
+    DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
 
 if(NOT WIN32)
     install(FILES ${CMAKE_CURRENT_BINARY_DIR}/sqlite3.pc
-        DESTINATION "lib/pkgconfig")
+    DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig")
 endif()
-
-install(TARGETS sqlite3
-    RUNTIME DESTINATION "bin"
-    ARCHIVE DESTINATION "lib"
-    LIBRARY DESTINATION "lib")
-

@jingyu
Copy link
Author

jingyu commented May 9, 2023

Thanks @nono303, I really appreciate all your help.

nono303 pushed a commit to nono303/win-build-scripts that referenced this pull request May 14, 2023
rhuijben added a commit to rhuijben/sqlite-amalgamation that referenced this pull request May 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants