Skip to content

Commit

Permalink
cmake added STRICT_MODE option
Browse files Browse the repository at this point in the history
It propagate strict compiler/linker options and Qt definitions using the
TinyOrm::CommonConfig interface library to the user code (highly
recommended; can also be set with the TINYORM_STRICT_MODE environment
variable).

 - updated vcpkg ports
 - updated docs
  • Loading branch information
silverqx committed Aug 30, 2023
1 parent 07ccc4f commit eac8957
Show file tree
Hide file tree
Showing 12 changed files with 92 additions and 3 deletions.
18 changes: 17 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,13 @@ a Debug configuration (-MTd, -MDd)" ON
"MSVC;NOT DEFINED CMAKE_MSVC_RUNTIME_LIBRARY" MSVC_RUNTIME_DYNAMIC-NOTFOUND
)

feature_option_environment(STRICT_MODE
"Propagate strict compiler/linker options and Qt definitions using \
the ${TinyOrm_ns}::${CommonConfig_target} interface library to the user code \
(highly recommended; can also be set with the TINYORM_STRICT_MODE environment variable)"
TINYORM_STRICT_MODE OFF
)

feature_option(VERBOSE_CONFIGURE
"Show information about PACKAGES_FOUND and PACKAGES_NOT_FOUND in the configure \
output" OFF
Expand Down Expand Up @@ -367,10 +374,19 @@ target_link_libraries(${TinyOrm_target}
Qt${QT_VERSION_MAJOR}::Core
Qt${QT_VERSION_MAJOR}::Sql
range-v3::range-v3
${TinyOrm_ns}::${CommonConfig_target}
)

# Conditional dependencies
if(STRICT_MODE)
target_link_libraries(${TinyOrm_target}
PUBLIC ${TinyOrm_ns}::${CommonConfig_target}
)
else()
target_link_libraries(${TinyOrm_target}
PRIVATE ${TinyOrm_ns}::${CommonConfig_target}
)
endif()

if(MYSQL_PING)
tiny_find_package(MySQL REQUIRED)
target_link_libraries(${TinyOrm_target} PRIVATE MySQL::MySQL)
Expand Down
22 changes: 22 additions & 0 deletions cmake/CommonModules/TinyFeatureOptions.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,28 @@ function(feature_option name description default)

endfunction()

# Helper function for coupling option() and add_feature_info() and use the default value
# from the given environment variable if defined, otherwise, use a value from the given
# default CMake variable
function(feature_option_environment name description environment_variable_name default)

# If an environment variable is defined, then use its value
if(DEFINED ENV{${environment_variable_name}})
if("$ENV{${environment_variable_name}}")
set(defaultValue ON)
else()
set(defaultValue OFF)
endif()

# Otherwise, use a value from the default CMake variable
else()
set(defaultValue "${default}")
endif()

feature_option(${name} "${description}" "${defaultValue}")

endfunction()

include(CMakeDependentOption)
# Helper function for coupling cmake_dependent_option() and add_feature_info()
macro(feature_option_dependent name description default depends force)
Expand Down
4 changes: 4 additions & 0 deletions cmake/Modules/TinyTestCommon.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ ${TINY_UNPARSED_ARGUMENTS}")
)
endif()

if(NOT STRICT_MODE)
target_link_libraries(${name} PRIVATE ${TinyOrm_ns}::${CommonConfig_target})
endif()

target_link_libraries(${name}
PRIVATE
Qt${QT_VERSION_MAJOR}::Test
Expand Down
1 change: 1 addition & 0 deletions cmake/vcpkg/ports/tinyorm-qt5/portfile.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ vcpkg_check_features(
inline-constants INLINE_CONSTANTS
mysql-ping MYSQL_PING
orm ORM
strict-mode STRICT_MODE
tom TOM
tom-example TOM_EXAMPLE
)
Expand Down
3 changes: 3 additions & 0 deletions cmake/vcpkg/ports/tinyorm-qt5/vcpkg.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@
"orm": {
"description": "Enable ORM-related source code (without it only the query builder is compiled)"
},
"strict-mode": {
"description": "Propagate strict compiler/linker options and Qt definitions"
},
"tom": {
"description": "Enable Tom-related source code (command-line interface)"
},
Expand Down
1 change: 1 addition & 0 deletions cmake/vcpkg/ports/tinyorm/portfile.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ vcpkg_check_features(
inline-constants INLINE_CONSTANTS
mysql-ping MYSQL_PING
orm ORM
strict-mode STRICT_MODE
tom TOM
tom-example TOM_EXAMPLE
)
Expand Down
3 changes: 3 additions & 0 deletions cmake/vcpkg/ports/tinyorm/vcpkg.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@
"orm": {
"description": "Enable ORM-related source code (without it only the query builder is compiled)"
},
"strict-mode": {
"description": "Propagate strict compiler/linker options and Qt definitions"
},
"tom": {
"description": "Enable Tom-related source code (command-line interface)"
},
Expand Down
4 changes: 4 additions & 0 deletions docs/building/hello-world.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,10 @@ And build.
cmake --build . --target all
```

:::tip
Enable the [`TINYORM_STRICT_MODE`](tinyorm.mdx#cmake-strict_mode-option) environment variable to produce better code and to follow good code practices.
:::

### Execute Hello world {#execute-hello-world-cmake}

Do not forget to add `TinyOrm0d.dll` on the path on Windows and on the `LD_LIBRARY_PATH` on Linux, so `HelloWorld` application can find it during execution, as is described [here](building/tinyorm.mdx#tinyorm-on-path-cmake).
Expand Down
19 changes: 19 additions & 0 deletions docs/building/tinyorm.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,24 @@ cd TinyORM-builds-cmake/build-debug
</TabItem>
</Tabs>

##### CMake `STRICT_MODE` option

The `STRICT_MODE` `CMake` configuration option was added in `TinyORM` `v0.35.0`. This option was added to avoid the propagation of aggressive strict warning compiler/linker options and Qt definitions from the `TinyORM` library to user code through the [`TinyOrm::CommonConfig`](https://github.com/silverqx/TinyORM/blob/main/cmake/CommonModules/TinyCommon.cmake) interface library.

`TinyORM` uses the strictest warning level options, virtually anything that can be enabled is enabled to produce a better code. I highly recommend enabling this option to produce better code and to follow good practices. It also helps to follow the `ISOCPP` [C++ Core Guidelines](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines) standards.

If you want to enable these strict warning options in your code, you can enable the `STRICT_MODE` `CMake` configuration option and they will be propagated to your code. You can also enabled it globally using the `TINYORM_STRICT_MODE` environment variable, and the value of this environment variable will be picked up during initial CMake configuration as the default value for the `STRICT_MODE` `CMake` configuration option.

You can achieve the same result by manually linking against the `TinyOrm::CommonConfig` interface library when the `STRICT_MODE` is set to `OFF`.

```cmake
target_link_libraries(<target> PRIVATE TinyOrm::CommonConfig)
```

:::info
The recommended way is to set the `TINYORM_STRICT_MODE` environment variable to `1`.
:::

#### Build TinyORM

And build. You don't have to install it, you can use the build tree directly if you want.
Expand Down Expand Up @@ -472,6 +490,7 @@ CMake multi-config generators like `Ninja Multi-Config` or `Visual Studio 16 201
| `MSVC_RUNTIME_DYNAMIC` | `ON` | Use MSVC dynamic runtime library (`-MD`) instead of static (`-MT`), also considers a Debug configuration (`-MTd`, `-MDd`).<br/><small>Available when: `MSVC AND NOT DEFINED CMAKE_MSVC_RUNTIME_LIBRARY`</small> |
| `MYSQL_PING` | `OFF` | Enable `Orm::MySqlConnection::pingDatabase()` method. |
| `ORM` | `ON` | Controls the compilation of all `ORM-related` source code, when this option is `disabled`, then only the `query builder` without `ORM` is compiled. Also excludes `ORM-related` unit tests. |
| `STRICT_MODE` | `OFF` | Controls propagation of strict compiler/linker options and Qt definitions using the `TinyOrm::CommonConfig` interface library to the user code.<br/><small>(highly recommended; can also be set with the `TINYORM_STRICT_MODE` environment variable; described [here](#cmake-strict_mode-option))</small>. |
| `TOM` | `ON` | Controls the compilation of all `Tom-related` source code, when this option is `disabled`, then it also excludes `Tom-related` unit tests. |
| `TOM_EXAMPLE` | `OFF` | Build the <abbr title='TinyORM Migrations'>`tom`</abbr> console application example. |
| `TOM_MIGRATIONS_DIR` | `-` | Default migrations path for the `make:migration` command, can be an absolute or relative path (to the <abbr title='Current working directory'>pwd</abbr>).<br/><small>Default value: `database/migrations` <small>(relative to the pwd)</small></small> |
Expand Down
7 changes: 6 additions & 1 deletion examples/tom/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -80,5 +80,10 @@ tiny_resource_and_manifest(${TomExample_target}
# Resolve and link dependencies
# ---

# Unconditional dependencies
if(NOT STRICT_MODE)
target_link_libraries(${TomExample_target}
PRIVATE ${TinyOrm_ns}::${CommonConfig_target}
)
endif()

target_link_libraries(${TomExample_target} PRIVATE ${TinyOrm_ns}::${TinyOrm_target})
6 changes: 6 additions & 0 deletions tests/TinyUtils/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,12 @@ tiny_resource_and_manifest(${TinyUtils_target}
# Resolve and link dependencies
# ---

if(NOT STRICT_MODE)
target_link_libraries(${TinyUtils_target}
PRIVATE ${TinyOrm_ns}::${CommonConfig_target}
)
endif()

target_link_libraries(${TinyUtils_target}
PUBLIC
${TinyOrm_ns}::${TinyOrm_target}
Expand Down
7 changes: 6 additions & 1 deletion tests/testdata_tom/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -82,5 +82,10 @@ tiny_resource_and_manifest(${TomTestData_target}
# Resolve and link dependencies
# ---

# Unconditional dependencies
if(NOT STRICT_MODE)
target_link_libraries(${TomTestData_target}
PRIVATE ${TinyOrm_ns}::${CommonConfig_target}
)
endif()

target_link_libraries(${TomTestData_target} PRIVATE ${TinyOrm_ns}::${TinyOrm_target})

0 comments on commit eac8957

Please sign in to comment.