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

Added code for version macro for code to check. #150

Merged
merged 10 commits into from
Jun 3, 2024
11 changes: 11 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ set(LIBTOOL_INTERFACE 2)
set(LIBTOOL_REVISION 3)
set(LIBTOOL_AGE 2)

set(GOTCHA_VERSION "(1, 0, 6)")


set(DEFAULT_SYMBOL_VISIBILITY hidden)

if(GOTCHA_ENABLE_TESTS)
Expand All @@ -22,6 +25,7 @@ if(GOTCHA_ENABLE_TESTS)
endif()
endif()
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include)
include_directories(${CMAKE_BINARY_DIR}/include)
add_subdirectory(include)
add_subdirectory(src)
if(GOTCHA_ENABLE_TESTS)
Expand Down Expand Up @@ -50,3 +54,10 @@ configure_package_config_file(
install(
FILES "${CMAKE_CURRENT_BINARY_DIR}/gotcha-config.cmake" "${CMAKE_CURRENT_BINARY_DIR}/gotcha-config-version.cmake"
DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/gotcha")

# Write the configure file
configure_file("${CMAKE_SOURCE_DIR}/cmake/gotcha_config.h.in"
"${CMAKE_CURRENT_BINARY_DIR}/include/gotcha/gotcha_config.h" @ONLY)
install(
FILES "${CMAKE_CURRENT_BINARY_DIR}/include/gotcha/gotcha_config.h"
DESTINATION "${gotcha_INSTALL_INCLUDE_DIR}/gotcha/gotcha_config.h")
10 changes: 10 additions & 0 deletions cmake/gotcha_config.h.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#ifndef GOTCHA_CONFIG_H
#define GOTCHA_CONFIG_H

#define GOTCHA_GET_VERSION(MAJOR, MINOR, PATCH) (MAJOR * 100000 + MINOR * 100 + PATCH)
#define GOTCHA_VERSION (GOTCHA_GET_VERSION @GOTCHA_VERSION@)
#define GOTCHA_VERSION_MAJOR (GOTCHA_VERSION / 100000)
#define GOTCHA_VERSION_MINOR ((GOTCHA_VERSION / 100) % 1000)
#define GOTCHA_VERSION_PATCH (GOTCHA_VERSION % 100)

#endif /* GOTCHA_CONFIG_H */
25 changes: 25 additions & 0 deletions docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -178,5 +178,30 @@ The default filter of gotcha selects all libraries loaded. This function set the

.. explicit external hyperlink targets

---------------------------
Using Gotcha Version Macros
---------------------------

The source version of GOTCHA is defined by the `GOTCHA_VERSION` macro which uses the XYYYZZ format.
**Available since version 1.0.7.**
Here, X signifies the major version, Y is the minor version, and Z is the patch.
Additionally, we define `GOTCHA_VERSION_MAJOR`, `GOTCHA_VERSION_MINOR`, and `GOTCHA_VERSION_PATCH` macros for convienience.
The codes can use the macros like


.. code-block:: c

#if GOTCHA_VERSION > 100006 // this will check of version greater than 1.0.6
#endif

#if GOTCHA_VERSION_MAJOR > 1 // this will check of version greater than 2.0.0
#endif

#if GOTCHA_VERSION > GOTCHA_GET_VERSION(1,0,6) // this will check of version greater than 1.0.6
#endif




.. _`gnu constructor`: https://gcc.gnu.org/onlinedocs/gcc-4.7.2/gcc/Function-Attributes.html
.. _symbol: https://refspecs.linuxfoundation.org/LSB_3.0.0/LSB-PDA/LSB-PDA.junk/symversion.html
4 changes: 2 additions & 2 deletions include/gotcha/gotcha.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
#ifndef GOTCHA_H
#define GOTCHA_H

#include <gotcha/gotcha_config.h>
#include <gotcha/gotcha_types.h>
#include <link.h>

#include "gotcha/gotcha_types.h"

#if defined(__cplusplus)
extern "C" {
#endif
Expand Down
21 changes: 21 additions & 0 deletions test/unit/gotcha_unit_tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,22 @@ Suite *gotcha_hash_suite() {
return s;
}

START_TEST(gotcha_version_check) {
ck_assert_msg(GOTCHA_GET_VERSION(1, 0, 3) > GOTCHA_GET_VERSION(1, 0, 2),
"Check GOTCHA_GET_VERSION failed");
ck_assert_msg(GOTCHA_VERSION >= GOTCHA_GET_VERSION(1, 0, 6),
"Check GOTCHA_VERSION failed");
}
END_TEST

Suite *gotcha_version_suite() {
Suite *s = suite_create("Gotcha Version");
TCase *version_case = configured_case_create("Basic tests");
tcase_add_test(version_case, gotcha_version_check);
suite_add_tcase(s, version_case);
return s;
}

////////////Launch///Tests////////////

int main() {
Expand All @@ -578,9 +594,14 @@ int main() {
SRunner *hash_runner = srunner_create(hash_suite);
srunner_run_all(hash_runner, CK_NORMAL);
num_fails += srunner_ntests_failed(hash_runner);
Suite *version_suite = gotcha_version_suite();
SRunner *version_runner = srunner_create(version_suite);
srunner_run_all(version_runner, CK_NORMAL);
num_fails += srunner_ntests_failed(version_runner);
srunner_free(core_runner);
srunner_free(libc_runner);
srunner_free(auxv_runner);
srunner_free(hash_runner);
srunner_free(version_runner);
return num_fails;
}
Loading