Skip to content

A simple, header-only C library designed to provide compile-time system information.

License

Notifications You must be signed in to change notification settings

KumarjitDas/kdapi

Repository files navigation

KDAPI

KDAPI is a simple, header-only C library designed to provide compile-time system information. This project targets both Windows and GNU/Linux (32-bit and 64-bit) platforms. The library aims to help developers easily retrieve details about the compiler, target operating system, target CPU, and endianness at compile-time.

Table of Contents

Features

  • Detects compiler type and version.
  • Identifies the target operating system.
  • Determines the target CPU architecture.
  • Determines the target architecture integer sizes (32 or 64 bit).
  • Determines the target architecture pointer(address) sizes (32 or 64 bit).
  • Checks system endianness.
  • Provides macros for import-export signatures, calling conventions, and extern indicators.
  • CMake target for ease of use with other CMake projects.

Requirements

To get started with this project, download and install the following.

  • Download and install git

    • If you use Windows, then go to this link and download and install the suitable version.

    • If you use any stable version of Debian/Ubuntu then run this command in your terminal

      sudo apt-get install git
    • If you use macOS then install homebrew if you don't already have it, then run this command in your terminal

      brew install git
  • Run the command to clone this repository

    git clone https://github.com/KumarjitDas/kdapi.git
  • Download and install a C compiler

    • If you use Windows 8/10/11 then download Visual Studio 2017/2019/2022 from this link or download and install a suitable version of clang from this link. For gcc, use the suitable MinGW version from this link.

    • If you use any stable version of Debian/Ubuntu then run these commands in your terminal to download and install clang and gcc compilers

      sudo apt install clang
      sudo apt install gcc
    • In macOS, clang is the default C compiler. To download and install gcc, run this command in your terminal

      brew install gcc
  • For building on Linux 32-bit targets on a 64-bit platform, gcc-multilib must be installed:

    sudo apt install gcc-multilib
  • Download and install CMake

    • For Windows 8/10/11 download from kitware or GitHub

    • If you use any stable version of Debian/Ubuntu then run these commands in your terminal to download and install cmake

      sudo apt install cmake
    • To download and install cmake on macOS, run this command in your terminal

      brew install cmake

Usage

  • Download the latest release of the project according to your system specification.

  • For CMake projects add the kdapi library cmake directory path to the CMAKE_PREFIX_PATH variable.

    • In CMake file

      # ...
      list(APPEND CMAKE_PREFIX_PATH path/to/kdapi/lib/cmake)
      # ...
    • In command line as an argument

      cmake ... -DCMAKE_PREFIX_PATH=path/to/kdapi/lib/cmake ...
  • Include the kdapi.h header file in your C project to use KDAPI.

    #include "kdapi.h"
  • To build the project from source using CMake you have to provide target platform information (KDAPI_TARGET_OS and KDAPI_TARGET_ARCH. For example:

    cmake -DKDAPI_TARGET_OS=windows -DKDAPI_TARGET_ARCH=x64 -G Ninja -S path/to/kdapi -B path/to/build
  • When building shared libraries

    • Add the KD_BUILDING_LIB macro at the top in your source file (.c) before "kdapi.h" inclusion

      #define KD_BUILDING_LIB 1
      #include "kdapi.h"
    • Pass the KD_DLL=1 to CMake through command line or in your main CMakeLists.txt file

      cmake -DKDAPI_TARGET_OS=windows -DKDAPI_TARGET_ARCH=x64 -DKD_DLL=1 -G Ninja -S path/to/kdapi -B path/to/build

      or,

      target_compile_definitions(mylib KD_DLL=1)

Example

You can find all the examples here. Here's a demo:

#include <stdio.h>
#include "kdapi.h"

int main() {
    printf("Compiler: %s\n", KD_COMP_STR);
    printf("Target Operating System: %s\n", KD_OS_STR);
    printf("Target CPU: %s\n", KD_CPU_STR);
    return 0;
}

Roadmap

See the open issues for a list of proposed features/functionalities (and known issues).

The list of features and functions implemented till now is given in Project Status.

Contributing

Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.

  • Fork this project [kdapi]

  • Create your Feature Branch

    git checkout -b feature/AmazingFeature
  • Commit your Changes

    git commit -m 'Added some AmazingFeature'
  • Push to the Branch

    git push origin feature/AmazingFeature
  • Create a pull request

Naming Convention

The KDAPI project follows a consistent naming convention to ensure readability and maintainability. Here are the key aspects of the naming convention used:

  • Macro Names: Macro names are written in uppercase letters with underscores separating words. They typically start with the prefix KD_.

    • Examples: KD_COMPILER_GCC, KD_OS_LINUX, KD_CPU_X86_64, KD_VERSION_MAJOR
  • Function-Like Macros: Function-like macros also follow the uppercase with underscores convention and use parentheses to indicate parameters.

    • Examples: KD_EXTERN_BEGIN, KD_EXTERN_END
  • Constants: Constants are defined using uppercase letters with underscores and often include a descriptive name or version number.

    • Examples: KD_VERSION_STR, KD_VERSION_MAJOR
  • File Names: File names are in lowercase and use underscores to separate words.

    • Examples: kdapi.h, example.c, setup_project.cmake

License

This project is distributed under the BSD 2-Clause License. See LICENSE for more information.

Project Status

List of functionalities/features implemented so far:

  • Compiler Detection: Macros for various compilers (Intel, GCC, LLVM, etc.).
  • OS Identification: Macros for target operating systems (Linux, Windows, etc.).
  • CPU Architecture Detection: Macros for target CPU architectures (x86, x64, ARM, etc.).
  • Architecture Integer Size: Macros for target CPU architecture integer size.
  • Architecture Pointer Size: Macros for target CPU architecture pointer(address) size.
  • Endianness Determination: Macros for little-endian and big-endian.
  • C-String Identifiers: Macros for human-readable C-string macros for compiler, OS, CPU, and endianness.
  • DLL Handling: Import-export macros for DLLs.
  • Calling Conventions: Macros for cdecl, stdcall, fastcall.
  • Build Configuration: CMake configuration files for shared and static builds.
  • Example Programs: Examples demonstrating library usage.

Contact

Twitter: @kumarjitdas1999

LinkedIn: Kumarjit Das

E-mail: kumarjitdas1999@gmail.com

Project link: KDAPI

Versioning

This project uses MAJOR, MINOR, and PATCH version numbers for versioning (vMAJOR.MINOR.PATCH).

  • MAJOR version number indicates new changes which may be incompatible with older versions.
  • MINOR version number indicates addition of backwards-compatible features.
  • PATCH version number indicates backwards-compatible bug fixes, or minor mistake fixes like spelling, character cases, punctuations, and indentation.

Changelog

The Changelog file contains all the information about the changes made to this project till now.

You can also view the raw version.