2022年12月31日 星期六

Linux Kernel(19.1)- /sys/class/gpio usage


要能使用/sys/class/gpio就要先開啟相關kernel config如下, 如提示所說, 該ABI已經棄用, 改由character device /dev/gpiochipN取代, 不過這裡還是會簡單交代一下相關資訊, 作為紀錄.
  │ CONFIG_GPIO_SYSFS:                                                                             │
  │                                                                                                │
  │ Say Y here to add the legacy sysfs interface for GPIOs.                                        │
  │                                                                                                │
  │ This ABI is deprecated. If you want to use GPIO from userspace,                                │
  │ use the character device /dev/gpiochipN with the appropriate                                   │
  │ ioctl() operations instead. The character device is always                                     │
  │ available.                                                                                     │
  │                                                                                                │
  │ Symbol: GPIO_SYSFS [=y]                                                                        │
  │ Type  : bool                                                                                   │
  │ Prompt: /sys/class/gpio/... (sysfs interface)                                                  │
  │   Location:                                                                                    │
  │     -> Device Drivers                                                                          │
  │       -> GPIO Support (GPIOLIB [=y])                                                           │
  │   Defined at drivers/gpio/Kconfig:61                                                           │
  │   Depends on: GPIOLIB [=y] && SYSFS [=y]                                                       │                                   

在設定玩kernel config之後, 重新編譯kernel, 就可以看見"/sys/class/gpio"目錄, 裡面幾個重要的檔案,
export Userspace may ask the kernel to export control of a GPIO to userspace by writing its number to this file.
unexport Reverses the effect of exporting to userspace.

"echo 19 > /sys/class/gpio/export" 後, 目錄/sys/class/gpio/gpio19/就會被創建出來, 裡面會包含
direction reads as either "in" or "out"
value reads as either 0 (low) or 1 (high)
edge reads as either "none", "rising", "falling", or "both"
active_low reads as either 0 (false) or 1 (true)

此外還可以透過debugfs來檢查當前的GPIO設定
/ # cat /sys/kernel/debug/gpio
gpiochip3: GPIOs 499-499, parent: platform/basic-mmio-gpio.3.auto, sys_flash:

gpiochip2: GPIOs 500-501, parent: platform/basic-mmio-gpio.2.auto, sys_mci:
 gpio-500 (                    |cd                  ) in  lo
 gpio-501 (                    |wp                  ) in  lo

gpiochip1: GPIOs 502-509, parent: platform/basic-mmio-gpio.1.auto, sys_led:
 gpio-502 (                    |?                   ) out lo
 gpio-503 (                    |?                   ) out lo
 gpio-504 (                    |?                   ) out lo
 gpio-505 (                    |?                   ) out lo
 gpio-506 (                    |?                   ) out hi
 gpio-507 (                    |?                   ) out lo
 gpio-508 (                    |?                   ) out lo
 gpio-509 (                    |?                   ) out lo

gpiochip0: GPIOs 510-511, parent: platform/10000000.sysreg, 10000000.sysreg:

/ # ls -al /sys/class/gpio/
total 0
drwxr-xr-x    2 0        0                0 Jan  3 00:40 .
drwxr-xr-x   36 0        0                0 Jan  3 00:40 ..
--w-------    1 0        0             4096 Jan  3 00:40 export
lrwxrwxrwx    1 0        0                0 Jan  3 00:40 gpiochip499 -> ../../devices/platform/10000000.sysreg/basic-mmio-gpio.3.auto/gpio/gpiochip499
lrwxrwxrwx    1 0        0                0 Jan  3 00:40 gpiochip500 -> ../../devices/platform/10000000.sysreg/basic-mmio-gpio.2.auto/gpio/gpiochip500
lrwxrwxrwx    1 0        0                0 Jan  3 00:40 gpiochip502 -> ../../devices/platform/10000000.sysreg/basic-mmio-gpio.1.auto/gpio/gpiochip502
lrwxrwxrwx    1 0        0                0 Jan  3 00:40 gpiochip510 -> ../../devices/platform/10000000.sysreg/gpio/gpiochip510
--w-------    1 0        0             4096 Jan  3 00:40 unexport

接下來export 510, 511, 然後在unexport 511, 設定510, 相關操作如下
/ # echo 510 > /sys/class/gpio/export
/ # echo 511 > /sys/class/gpio/export
/ # ls -al /sys/class/gpio/
total 0
drwxr-xr-x    2 0        0                0 Jan  3 00:40 .
drwxr-xr-x   36 0        0                0 Jan  3 00:40 ..
--w-------    1 0        0             4096 Jan  3 00:43 export
lrwxrwxrwx    1 0        0                0 Jan  3 00:42 gpio510 -> ../../devices/platform/10000000.sysreg/gpiochip0/gpio/gpio510
lrwxrwxrwx    1 0        0                0 Jan  3 00:43 gpio511 -> ../../devices/platform/10000000.sysreg/gpiochip0/gpio/gpio511
lrwxrwxrwx    1 0        0                0 Jan  3 00:40 gpiochip499 -> ../../devices/platform/10000000.sysreg/basic-mmio-gpio.3.auto/gpio/gpiochip499
lrwxrwxrwx    1 0        0                0 Jan  3 00:40 gpiochip500 -> ../../devices/platform/10000000.sysreg/basic-mmio-gpio.2.auto/gpio/gpiochip500
lrwxrwxrwx    1 0        0                0 Jan  3 00:40 gpiochip502 -> ../../devices/platform/10000000.sysreg/basic-mmio-gpio.1.auto/gpio/gpiochip502
lrwxrwxrwx    1 0        0                0 Jan  3 00:40 gpiochip510 -> ../../devices/platform/10000000.sysreg/gpio/gpiochip510
--w-------    1 0        0             4096 Jan  3 00:40 unexport

/ # echo 511 > /sys/class/gpio/unexport
/ # ls -al /sys/class/gpio/
total 0
drwxr-xr-x    2 0        0                0 Jan  3 00:40 .
drwxr-xr-x   36 0        0                0 Jan  3 00:40 ..
--w-------    1 0        0             4096 Jan  3 00:43 export
lrwxrwxrwx    1 0        0                0 Jan  3 00:42 gpio510 -> ../../devices/platform/10000000.sysreg/gpiochip0/gpio/gpio510
lrwxrwxrwx    1 0        0                0 Jan  3 00:40 gpiochip499 -> ../../devices/platform/10000000.sysreg/basic-mmio-gpio.3.auto/gpio/gpiochip499
lrwxrwxrwx    1 0        0                0 Jan  3 00:40 gpiochip500 -> ../../devices/platform/10000000.sysreg/basic-mmio-gpio.2.auto/gpio/gpiochip500
lrwxrwxrwx    1 0        0                0 Jan  3 00:40 gpiochip502 -> ../../devices/platform/10000000.sysreg/basic-mmio-gpio.1.auto/gpio/gpiochip502
lrwxrwxrwx    1 0        0                0 Jan  3 00:40 gpiochip510 -> ../../devices/platform/10000000.sysreg/gpio/gpiochip510
--w-------    1 0        0             4096 Jan  3 00:43 unexport

/ # grep "" /sys/class/gpio/gpio510/*
/sys/class/gpio/gpio510/active_low:0
/sys/class/gpio/gpio510/direction:in
/sys/class/gpio/gpio510/value:0

/ # echo out > /sys/class/gpio/gpio510/direction
/ # grep "" /sys/class/gpio/gpio510/*
/sys/class/gpio/gpio510/active_low:0
/sys/class/gpio/gpio510/direction:out
/sys/class/gpio/gpio510/value:0

/ # cat /sys/kernel/debug/gpio
gpiochip3: GPIOs 499-499, parent: platform/basic-mmio-gpio.3.auto, sys_flash:

gpiochip2: GPIOs 500-501, parent: platform/basic-mmio-gpio.2.auto, sys_mci:
 gpio-500 (                    |cd                  ) in  lo
 gpio-501 (                    |wp                  ) in  lo

gpiochip1: GPIOs 502-509, parent: platform/basic-mmio-gpio.1.auto, sys_led:
 gpio-502 (                    |?                   ) out lo
 gpio-503 (                    |?                   ) out lo
 gpio-504 (                    |?                   ) out lo
 gpio-505 (                    |?                   ) out lo
 gpio-506 (                    |?                   ) out hi
 gpio-507 (                    |?                   ) out hi
 gpio-508 (                    |?                   ) out lo
 gpio-509 (                    |?                   ) out lo

gpiochip0: GPIOs 510-511, parent: platform/10000000.sysreg, 10000000.sysreg:
 gpio-510 (                    |sysfs               ) out lo

/ # grep "" /sys/class/gpio/gpiochip*/*
/sys/class/gpio/gpiochip499/base:499
/sys/class/gpio/gpiochip499/label:sys_flash
/sys/class/gpio/gpiochip499/ngpio:1
/sys/class/gpio/gpiochip500/base:500
/sys/class/gpio/gpiochip500/label:sys_mci
/sys/class/gpio/gpiochip500/ngpio:2
/sys/class/gpio/gpiochip502/base:502
/sys/class/gpio/gpiochip502/label:sys_led
/sys/class/gpio/gpiochip502/ngpio:8
/sys/class/gpio/gpiochip510/base:510
/sys/class/gpio/gpiochip510/label:10000000.sysreg
/sys/class/gpio/gpiochip510/ngpio:2



2022年8月14日 星期日

RFC 1319 - MD2 Message-Digest Algorithm


Message Digest 演算法是以隨意長度的message作為input, 並產生128-bit的"fingerprint"或"message "digest". 雖然RFC後面有附上實作, 但是網路上這篇比較容易閱讀, 有助於理解MD2的演算法,
/**
 * @file md2.c
 * @author .ukasz Grudnik (https://github.com/lukaszgrudnik)
 * @brief MD2 algorithm based on RFC documentation https://datatracker.ietf.org/doc/html/rfc1319
 * @version 0.1
 * @date 2022-06-26
 *
 * @copyright Copyright (c) 2022
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// This step uses a 256-byte "random" permutation constructed from the
//    digits of pi. Let S[i] denote the i-th element of this table.
static unsigned char S[256] = {
    41, 46, 67, 201, 162, 216, 124, 1, 61, 54, 84, 161, 236, 240, 6,
    19, 98, 167, 5, 243, 192, 199, 115, 140, 152, 147, 43, 217, 188,
    76, 130, 202, 30, 155, 87, 60, 253, 212, 224, 22, 103, 66, 111, 24,
    138, 23, 229, 18, 190, 78, 196, 214, 218, 158, 222, 73, 160, 251,
    245, 142, 187, 47, 238, 122, 169, 104, 121, 145, 21, 178, 7, 63,
    148, 194, 16, 137, 11, 34, 95, 33, 128, 127, 93, 154, 90, 144, 50,
    39, 53, 62, 204, 231, 191, 247, 151, 3, 255, 25, 48, 179, 72, 165,
    181, 209, 215, 94, 146, 42, 172, 86, 170, 198, 79, 184, 56, 210,
    150, 164, 125, 182, 118, 252, 107, 226, 156, 116, 4, 241, 69, 157,
    112, 89, 100, 113, 135, 32, 134, 91, 207, 101, 230, 45, 168, 2, 27,
    96, 37, 173, 174, 176, 185, 246, 28, 70, 97, 105, 52, 64, 126, 15,
    85, 71, 163, 35, 221, 81, 175, 58, 195, 92, 249, 206, 186, 197,
    234, 38, 44, 83, 13, 110, 133, 40, 132, 9, 211, 223, 205, 244, 65,
    129, 77, 82, 106, 220, 55, 200, 108, 193, 171, 250, 36, 225, 123,
    8, 12, 189, 177, 74, 120, 136, 149, 139, 227, 99, 232, 109, 233,
    203, 213, 254, 59, 0, 29, 57, 242, 239, 183, 14, 102, 88, 208, 228,
    166, 119, 114, 248, 235, 117, 75, 10, 49, 68, 80, 180, 143, 237,
    31, 26, 219, 153, 141, 51, 159, 17, 131, 20
};

int main(int argc, char *argv[])
{

    if (argc == 1) {
        printf("error: Type message to hash\n");
        printf("./a.out <message>\n");
        return -1;
    }

    // Step 1. Append Padding Bytes
    int p, N, n;


    // The message is "padded" (extended) so that its length (in bytes) is
    //   congruent to 0, modulo 16.
    n = strlen(argv[1]) * sizeof(unsigned char);
    p = 16 - n % 16;
    N = n + p;

    unsigned char *M = (unsigned char *)malloc(N);

    // Padding is performed as follows: "i" bytes of value "i" are appended
    //   to the message so that the length in bytes of the padded message
    //   becomes congruent to 0, modulo 16. At least one byte and at most 16
    //   16 bytes are appended.
    memcpy(M, argv[1], n);
    memset(M+n, p, p);

    // Step 2. Append Checksum
    // Clear checksum.
    unsigned char C[16];
    memset(C, 0, 16);

    unsigned char c;
    // Set L to 0.
    unsigned char L = 0;

    // Process each 16-word block.
    for (int i = 0; i <= N / 16 - 1; i++) {
        for (int j = 0; j <= 15; j++) {
            c = M[i * 16 + j]; // Set c to M[i*16+j]
            C[j] ^= S[c ^ L];  // Set C[j] to S[c xor L]
            L = C[j];          // Set L to C[j]
        }
    }

    // The 16-byte checksum C[0 ... 15] is appended to the message
    // Let M[0] with checksum), where N' = N + 16.
    int N_ = N + 16;
    unsigned char *M_ = (unsigned char *)malloc(N_);

    memcpy(M_, M, N);
    memcpy(M_ + N, C, 16);

    //  Step 3. Initialize MD Buffer
    //  A 48-byte buffer X is used to compute the message digest.
    //  The buffer is initialized to zero.
    unsigned char X[48] = {0};

    //  Step 4. Process Message in 16-Byte Blocks
    unsigned int t = 0;

    for (int m = 0; m < 16; m++) {
        printf("%d ", M_[N + m]);
    }

    // Process each 16-word block
    for (int i = 0; i <= (N_ / 16) - 1; i++) {
        // Copy block i into X.
        for (int j = 0; j <= 15; j++) {
            X[16 + j] = M_[16 * i + j]; // Set X[16+j] to M[i*16+j].
            X[32 + j] = (X[16 + j] ^ X[j]); // Set X[32+j] to (X[16+j] xor X[j])
        }

        // Set t to 0.
        t = 0;
        // Do 18 rounds.
        for (int j = 0; j <= 17; j++) {
            for (int k = 0; k <= 47; k++) {
                t = X[k] ^= S[t]; // Set t and X[k] to (X[k] xor S[t]).
            }
            t = (t + j) % 256; // Set t to (t+j) modulo 256.
        }
    }

    // Step 5. Output
    printf("\nHash:\n");
    for (int i = 0; i < 48; i++) {
        printf("%02x ", X[i]);
    }
    return 0;
}

    參考資料:
  • https://www.ietf.org/rfc/rfc1319.txt
  • https://github.com/lukaszgrudnik/MD2
  • https://nickthecrypt.medium.com/cryptography-hash-method-md2-message-digest-2-step-by-step-explanation-made-easy-with-python-10faa2e35e85




2022年7月10日 星期日

CMake - Step 4: Installing and Testing - Testing


這章節要來解釋如何在CMAKE中加入auto test功能, 主要是透過enable_testing()啟動testing功能, 並透過add_test()間接呼叫ctest執行測試, 以及set_tests_properties(PROPERTIES PASS_REGULAR_EXPRESSION)來驗證執行結果
# Require a minimum version of cmake.
# cmake_minimum_required(VERSION <min>>[...<policy_max>] [FATAL_ERROR])
cmake_minimum_required(VERSION 3.10)

# Set the name of the project.
# project(<PROJECT-NAME< [<anguage-name>...])
# project(<PROJECT-NAME>
#         [VERSION <major>[.<minor>[.<patch>[.<tweak>]]]]
#         [DESCRIPTION <project-description-string>]
#         [LANGUAGES <language-name>...])
project(Tutorial VERSION 0.1.2.3 DESCRIPTION "This is Brook 1st CMake Lib")

# Add an executable to the project using the specified source files.
# add_executable(<name> [WIN32] [MACOSX_BUNDLE]
#                [EXCLUDE_FROM_ALL]
#                [source1] [source2 ...])
add_executable(Tutorial hello.c)

# option(<variable> "" [value])
option(USE_MYMATH "Use tutorial provided math implementation" ON)

# configure_file(<input> <output>
#               [NO_SOURCE_PERMISSIONS | USE_SOURCE_PERMISSIONS |
#                FILE_PERMISSIONS <permissions>...]
#               [COPYONLY] [ESCAPE_QUOTES] [@ONLY]
#               [NEWLINE_STYLE [UNIX|DOS|WIN32|LF|CRLF] ])
# configure a header file to pass some of the CMake settings
# to the source code
configure_file(Config.h.in Config.h)

if (USE_MYMATH)
# Add a subdirectory to the build.
# add_subdirectory(source_dir [binary_dir]
#                        [EXCLUDE_FROM_ALL])
add_subdirectory(mymath01)

# list(APPEND <list> [<element> ...])
# Appends elements to the list.
# If no variable named <list> exists in the current
# scope its value is treated as empty and the elements
# are appended to that empty list.
list(APPEND EXTRA_LIBS MyMathLib1)
else()
list(APPEND EXTRA_LIBS "m")
endif()

# Specify libraries or flags to use when linking a given target and/or its dependents.
# target_link_libraries(<target>
#                      <PRIVATE|PUBLIC|INTERFACE> ...
#                      [<PRIVATE|PUBLIC|INTERFACE> ...]...)
target_link_libraries(Tutorial PUBLIC ${EXTRA_LIBS})

# Add include directories to a target.
# target_include_directories(<target> [SYSTEM] [AFTER|BEFORE]
#   <INTERFACE|PUBLIC|PRIVATE> [items1...]
#   [<INTERFACE|PUBLIC|PRIVATE> [items2...] ...])
target_include_directories(Tutorial PUBLIC "${PROJECT_BINARY_DIR}")

# Enable testing for current directory and below.
enable_testing()

# Start recording a function for later invocation as a command.
# function(<name> [<arg1> ...])
#         <commands>
# endfunction()
function(do_test target arg result)
  # Add a test to the project to be run by ctest(1).
  #add_test(NAME <name> COMMAND <command> [<arg>...]
  #         [CONFIGURATIONS <config>...]
  #         [WORKING_DIRECTORY <dir>]
  #          [COMMAND_EXPAND_LISTS])
  # Adds a test called <name>
  add_test(NAME Comp${arg} COMMAND ${target} ${arg})

  # Set a property of the tests.
  # set_tests_properties(test1 [test2...] PROPERTIES prop1 value1 prop2 value2)
  set_tests_properties(Comp${arg}
      PROPERTIES PASS_REGULAR_EXPRESSION ${result}
  )
endfunction()

do_test(Tutorial 4 "mymath: 16")

上述範例主要透過do_test(Tutorial 4 "mymath: 16")執行測試, 而do_test為我們自訂的function, 透過add_test(NAME Comp${arg} COMMAND ${target} ${arg})執行ctest與set_tests_properties(Comp${arg} PROPERTIES PASS_REGULAR_EXPRESSION ${result})驗證執行結果, 這裡的${result}就等於"mymath: 16", 要根據輸出結果作對應修改.
執行結果如下
[brook@:~/Projects/cmake/04-2]$ mkdir build && cd build
[brook@:~/Projects/cmake/04-2/build]$ cmake ../src/ && make all test ARGS="-V"
-- The C compiler identification is GNU 7.5.0
-- The CXX compiler identification is GNU 7.5.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /build/brook/Projects/cmake/04-2/build
Scanning dependencies of target MyMathLib1
[ 25%] Building C object mymath01/CMakeFiles/MyMathLib1.dir/mymath01.c.o
[ 50%] Linking C static library libMyMathLib1.a
[ 50%] Built target MyMathLib1
Scanning dependencies of target Tutorial
[ 75%] Building C object CMakeFiles/Tutorial.dir/hello.c.o
/build/brook/Projects/cmake/04-2/src/hello.c:6:9: note: #pragma message: use mymath
 #pragma message("use mymath")
         ^~~~~~~
[100%] Linking C executable Tutorial
[100%] Built target Tutorial
Running tests...
UpdateCTestConfiguration  from :/build/brook/Projects/cmake/04-2/build/DartConfiguration.tcl
UpdateCTestConfiguration  from :/build/brook/Projects/cmake/04-2/build/DartConfiguration.tcl
Test project /build/brook/Projects/cmake/04-2/build
Constructing a list of tests
Done constructing a list of tests
Updating test list for fixtures
Added 0 tests to meet fixture requirements
Checking test dependency graph...
Checking test dependency graph end
test 1
    Start 1: Comp4

1: Test command: /build/brook/Projects/cmake/04-2/build/Tutorial "4"
1: Test timeout computed to be: 9.99988e+06
1: mymath: 16
1/1 Test #1: Comp4 ............................   Passed    0.01 sec

100% tests passed, 0 tests failed out of 1

Total Test time (real) =   0.01 sec
[brook@:~/Projects/cmake/04-2/build]$ cmake ../src/ && make all test
-- Configuring done
-- Generating done
-- Build files have been written to: /build/brook/Projects/cmake/04-2/build
[ 50%] Built target MyMathLib1
[100%] Built target Tutorial
Running tests...
Test project /build/brook/Projects/cmake/04-2/build
    Start 1: Comp4
1/1 Test #1: Comp4 ............................   Passed    0.00 sec

100% tests passed, 0 tests failed out of 1

Total Test time (real) =   0.01 sec


利用make test ARGS="-V"多印一些ctest的output, 這樣比較容易debug


2022年7月2日 星期六

CMake - Step 4: Installing and Testing - Installing


install() 用來產生installation rules, 這個章節主要談兩個install指令,install(TARGETS)與install(FILES).cmake的install選像是在3.15之後才支援, 之前的版本請直接用"make install [DESTDIR=<destdir>]
          
[brook@:~/Projects/cmake/04]$ tree
.
`-- src
    |-- CMakeLists.txt
    |-- Config.h.in
    |-- hello.c
    `-- mymath01
        |-- CMakeLists.txt
        |-- mymath01.c
        `-- mymath01.h

2 directories, 6 files
[brook@:~/Projects/cmake/04]$ cat src/CMakeLists.txt
# Require a minimum version of cmake.
# cmake_minimum_required(VERSION <min>[...<policy_max>] [FATAL_ERROR])
cmake_minimum_required(VERSION 3.10)

# Set the name of the project.
# project(<PROJECT-NAME< [<anguage-name>...])
# project(<PROJECT-NAME>
#         [VERSION <major>[.<minor>[.<patch>[.<tweak>]]]]
#         [DESCRIPTION <project-description-string>]
#         [LANGUAGES <language-name>...])
project(Tutorial VERSION 0.1.2.3 DESCRIPTION "This is Brook 1st CMake Lib")

# Add an executable to the project using the specified source files.
# add_executable(<name> [WIN32] [MACOSX_BUNDLE]
#                [EXCLUDE_FROM_ALL]
#                [source1] [source2 ...])
add_executable(Tutorial hello.c)

# option(<variable> "<help_text>" [value])
option(USE_MYMATH "Use tutorial provided math implementation" ON)

# configure_file(<input> <output>
#               [NO_SOURCE_PERMISSIONS | USE_SOURCE_PERMISSIONS |
#                FILE_PERMISSIONS <permissions>...]
#               [COPYONLY] [ESCAPE_QUOTES] [@ONLY]
#               [NEWLINE_STYLE [UNIX|DOS|WIN32|LF|CRLF] ])
# configure a header file to pass some of the CMake settings
# to the source code
configure_file(Config.h.in Config.h)

if (USE_MYMATH)
# Add a subdirectory to the build.
# add_subdirectory(source_dir [binary_dir]
#                        [EXCLUDE_FROM_ALL])
add_subdirectory(mymath01)

# list(APPEND <list> [<element> ...])
# Appends elements to the list.
# If no variable named <list> exists in the current
# scope its value is treated as empty and the elements
# are appended to that empty list.
list(APPEND EXTRA_LIBS MyMathLib1)
else()
list(APPEND EXTRA_LIBS "m")
endif()

# Specify libraries or flags to use when linking a given target and/or its dependents.
# target_link_libraries(<target>
#                      <PRIVATE|PUBLIC|INTERFACE> ...
#                      [<PRIVATE|PUBLIC|INTERFACE> ...]...)
target_link_libraries(Tutorial PUBLIC ${EXTRA_LIBS})

# Add include directories to a target.
# target_include_directories(<target> [SYSTEM] [AFTER|BEFORE]
#   <INTERFACE|PUBLIC|PRIVATE> [items1...]
#   [<INTERFACE|PUBLIC|PRIVATE> [items2...] ...])
target_include_directories(Tutorial PUBLIC "${PROJECT_BINARY_DIR}")

install(TARGETS Tutorial DESTINATION bin)
install(FILES "${PROJECT_BINARY_DIR}/Config.h" DESTINATION include)

[brook@:~/Projects/cmake/04]$ cat src/mymath01/CMakeLists.txt
# Add a library to the project using the specified source files.
# add_library(<name> [STATIC | SHARED | MODULE]
#             [EXCLUDE_FROM_ALL]
#                       source1 [source2 ...])
add_library(MyMathLib1 mymath01.c)

# target_include_directories(<target> [SYSTEM] [AFTER|BEFORE]
#  <INTERFACE|PUBLIC|PRIVATE> [items1...]
#    [<INTERFACE|PUBLIC|PRIVATE> [items2...] ...])
# target_include_directories(MyMathLib1 PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
target_include_directories(MyMathLib1 INTERFACE ${CMAKE_CURRENT_SOURCE_DIR})

# install(TARGETS targets... [EXPORT <export-name>]
#        [RUNTIME_DEPENDENCIES args...|RUNTIME_DEPENDENCY_SET <set-name>]
#        [[ARCHIVE|LIBRARY|RUNTIME|OBJECTS|FRAMEWORK|BUNDLE|
#          PRIVATE_HEADER|PUBLIC_HEADER|RESOURCE|FILE_SET <set-name>]
#          [DESTINATION <dir>]
#          [PERMISSIONS permissions...]
#          [CONFIGURATIONS [Debug|Release|...]]
#          [COMPONENT <component>]
#          [NAMELINK_COMPONENT <component>]
#          [OPTIONAL] [EXCLUDE_FROM_ALL]
#          [NAMELINK_ONLY|NAMELINK_SKIP]
#          ] [...]
#         [INCLUDES DESTINATION [<dir> ...]]
install(TARGETS MyMathLib1 DESTINATION lib)

# install(<FILES|PROGRAMS> files...
#        TYPE <type> | DESTINATION <dir>
#              [PERMISSIONS permissions...]
#              [CONFIGURATIONS [Debug|Release|...]]
#              [COMPONENT <component>]
#              [RENAME <name>] [OPTIONAL] [EXCLUDE_FROM_ALL])
install(FILES mymath01.h DESTINATION include)

[brook@:~/Projects/cmake/04]$ mkdir build && cd build
[brook@:~/Projects/cmake/04/build]$ cmake ../src
-- The C compiler identification is GNU 7.5.0
-- The CXX compiler identification is GNU 7.5.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /build/brook/Projects/cmake/04/build
[brook@:~/Projects/cmake/04/build]$ cmake --build .
Scanning dependencies of target MyMathLib1
[ 25%] Building C object mymath01/CMakeFiles/MyMathLib1.dir/mymath01.c.o
[ 50%] Linking C static library libMyMathLib1.a
[ 50%] Built target MyMathLib1
Scanning dependencies of target Tutorial
[ 75%] Building C object CMakeFiles/Tutorial.dir/hello.c.o
/build/brook/Projects/cmake/04/src/hello.c:6:9: note: #pragma message: use mymath
 #pragma message("use mymath")
         ^~~~~~~
[100%] Linking C executable Tutorial
[100%] Built target Tutorial
[brook@:~/Projects/cmake/04/build]$ make install
[ 50%] Built target MyMathLib1
[100%] Built target Tutorial
Install the project...
-- Install configuration: ""
-- Installing: /usr/local/bin/Tutorial
CMake Error at cmake_install.cmake:47 (file):
  file INSTALL cannot copy file
  "/build/brook/Projects/cmake/04/build/Tutorial" to
  "/usr/local/bin/Tutorial".


Makefile:117: recipe for target 'install' failed
make: *** [install] Error 1
[brook@:~/Projects/cmake/04/build]$ make install DESTDIR=/build/brook/Projects/cmake
[ 50%] Built target MyMathLib1
[100%] Built target Tutorial
Install the project...
-- Install configuration: ""
-- Installing: /build/brook/Projects/cmake/usr/local/bin/Tutorial
-- Installing: /build/brook/Projects/cmake/usr/local/include/Config.h
-- Installing: /build/brook/Projects/cmake/usr/local/lib/libMyMathLib1.a
-- Installing: /build/brook/Projects/cmake/usr/local/include/mymath01.h
[brook@:~/Projects/cmake/04/build]$ tree /build/brook/Projects/cmake/usr/
/build/brook/Projects/cmake/usr/
`-- local
    |-- bin
    |   `-- Tutorial
    |-- include
    |   |-- Config.h
    |   `-- mymath01.h
    `-- lib
        `-- libMyMathLib1.a

4 directories, 4 files


這裡的install主要用了Target/FILES兩個選項, 再搭配DESTINATION參數, DESTINATION是指要安裝到哪個目錄去, 可以是相對路徑或是絕對路徑(relative or absolute paths). 可以透過CMAKE_INSTALL_PREFIX在cmake時設定prefix, 或在make時透過DESTDIR去改變
          
[brook@:~/Projects/cmake/04/src]$ cmake -DCMAKE_INSTALL_PREFIX=/build/brook/Projects/cmake
-- The C compiler identification is GNU 7.5.0
-- The CXX compiler identification is GNU 7.5.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /build/brook/Projects/cmake/04/src
[brook@:~/Projects/cmake/04/src]$ cmake --build .
Scanning dependencies of target MyMathLib1
[ 25%] Building C object mymath01/CMakeFiles/MyMathLib1.dir/mymath01.c.o
[ 50%] Linking C static library libMyMathLib1.a
[ 50%] Built target MyMathLib1
Scanning dependencies of target Tutorial
[ 75%] Building C object CMakeFiles/Tutorial.dir/hello.c.o
/build/brook/Projects/cmake/04/src/hello.c:6:9: note: #pragma message: use mymath
 #pragma message("use mymath")
         ^~~~~~~
[100%] Linking C executable Tutorial
[100%] Built target Tutorial
[brook@:~/Projects/cmake/04/src]$ make install
[ 50%] Built target MyMathLib1
[100%] Built target Tutorial
Install the project...
-- Install configuration: ""
-- Installing: /build/brook/Projects/cmake/bin/Tutorial
-- Installing: /build/brook/Projects/cmake/include/Config.h
-- Installing: /build/brook/Projects/cmake/lib/libMyMathLib1.a
-- Installing: /build/brook/Projects/cmake/include/mymath01.h
[brook@:~/Projects/cmake/04/src]$ make install DESTDIR=/build/brook/Projects/cmake/04
[ 50%] Built target MyMathLib1
[100%] Built target Tutorial
Install the project...
-- Install configuration: ""
-- Installing: /build/brook/Projects/cmake/04/build/brook/Projects/cmake/bin/Tutorial
-- Installing: /build/brook/Projects/cmake/04/build/brook/Projects/cmake/include/Config.h
-- Installing: /build/brook/Projects/cmake/04/build/brook/Projects/cmake/lib/libMyMathLib1.a
-- Installing: /build/brook/Projects/cmake/04/build/brook/Projects/cmake/include/mymath01.h



2022年6月26日 星期日

CMake - Step 3: Adding Usage Requirements for a Library


這個章節想表達, CMAKE可以透過以下這幾個API, 對include或link有更多的控制
  • target_compile_definitions()
  • target_compile_options()
  • target_include_directories()
  • target_link_libraries()
不過這章只提到target_include_directories(), 其INTERFACE/PUBLIC屬性可以讓consumer能找到該header file, 所以之前Step 2的list(APPEND EXTRA_INCLUDES "${PROJECT_SOURCE_DIR}/MathFunctions")就可以移除了
[brook@:~/Projects/cmake/03]$ tree src
src
|-- CMakeLists.txt
|-- Config.h.in
|-- hello.c
`-- mymath01
    |-- CMakeLists.txt
    |-- mymath01.c
    `-- mymath01.h

1 directory, 6 files

[brook@:~/Projects/cmake/03]$ cat src/CMakeLists.txt
# Require a minimum version of cmake.
# cmake_minimum_required(VERSION <min>>[...<policy_max>] [FATAL_ERROR])
cmake_minimum_required(VERSION 3.10)

# Set the name of the project.
# project(<PROJECT-NAME< [<anguage-name>...])
# project(<PROJECT-NAME>
#         [VERSION <major>[.<minor>[.<patch>[.<tweak>]]]]
#         [DESCRIPTION <project-description-string>]
#         [LANGUAGES <language-name>...])
project(Tutorial VERSION 0.1.2.3 DESCRIPTION "This is Brook 1st CMake Lib")

# Add an executable to the project using the specified source files.
# add_executable(<name> [WIN32] [MACOSX_BUNDLE]
#                [EXCLUDE_FROM_ALL]
#                [source1] [source2 ...])
add_executable(Tutorial hello.c)

# option(<variable> "<help_text>" [value])
option(USE_MYMATH "Use tutorial provided math implementation" ON)

# configure_file(<input> <output>
#               [NO_SOURCE_PERMISSIONS | USE_SOURCE_PERMISSIONS |
#                FILE_PERMISSIONS <permissions>...]
#               [COPYONLY] [ESCAPE_QUOTES] [@ONLY]
#               [NEWLINE_STYLE [UNIX|DOS|WIN32|LF|CRLF] ])
# configure a header file to pass some of the CMake settings
# to the source code
configure_file(Config.h.in Config.h)

if (USE_MYMATH)
# Add a subdirectory to the build.
# add_subdirectory(source_dir [binary_dir]
#                        [EXCLUDE_FROM_ALL])
add_subdirectory(mymath01)

# list(APPEND <list> [<element> ...])
# Appends elements to the list.
# If no variable named <list> exists in the current
# scope its value is treated as empty and the elements
# are appended to that empty list.
list(APPEND EXTRA_LIBS MyMathLib1)
else()
list(APPEND EXTRA_LIBS "m")
endif()

# Specify libraries or flags to use when linking a given target and/or its dependents.
# target_link_libraries(<target>
#                      <PRIVATE|PUBLIC|INTERFACE> <item>...
#                      [<PRIVATE|PUBLIC|INTERFACE> <item>...]...)
target_link_libraries(Tutorial PUBLIC ${EXTRA_LIBS})

# Add include directories to a target.
# target_include_directories(<target> [SYSTEM] [AFTER|BEFORE]
#   <INTERFACE|PUBLIC|PRIVATE> [items1...]
#   [<INTERFACE|PUBLIC|PRIVATE> [items2...] ...])
target_include_directories(Tutorial PUBLIC "${PROJECT_BINARY_DIR}")
[brook@:~/Projects/cmake/03]$ cat src/Config.h.in
#cmakedefine USE_MYMATH
[brook@:~/Projects/cmake/03]$ cat src/hello.c
#include <stdio.h>
#include <stdlib.h>
#include "Config.h"

#ifdef USE_MYMATH
#pragma message("use mymath")
#include "mymath01.h"
#else
#pragma message("use system")
#include 
#endif

int main(int argc, char *argv[])
{
#ifdef USE_MYMATH
        printf("mymath: %d\n", squar(atoi(argv[1])));
#else
        printf("system: %d\n", (int)pow(atoi(argv[1]), 2));
#endif
        return 0;
}

[brook@:~/Projects/cmake/03]$ cat src/mymath01/CMakeLists.txt
# Add a library to the project using the specified source files.
# add_library(<name> [STATIC | SHARED | MODULE]
#             [EXCLUDE_FROM_ALL]
#                       source1 [source2 ...])
add_library(MyMathLib1 mymath01.c)

# target_include_directories(<target> [SYSTEM] [AFTER|BEFORE]
#  <INTERFACE|PUBLIC|PRIVATE> [items1...]
#    [<INTERFACE|PUBLIC|PRIVATE> [items2...] ...])
#Remember INTERFACE means things that consumers require but the producer doesn't.
# target_include_directories(MyMathLib1 PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
target_include_directories(MyMathLib1 INTERFACE ${CMAKE_CURRENT_SOURCE_DIR})
[brook@:~/Projects/cmake/03]$ cat src/mymath01/mymath01.h
#ifndef MYMATH01_H
#define MYMATH01_H

int squar(int a);

#endif
[brook@:~/Projects/cmake/03]$ cat src/mymath01/mymath01.c
#include "mymath01.h"

int squar(int a)
{
        return a*a;
}

執行結果
[brook@:~/Projects/cmake/03]$ mkdir build && cd build
[brook@:~/Projects/cmake/03/build]$ cmake ../src
-- The C compiler identification is GNU 7.5.0
-- The CXX compiler identification is GNU 7.5.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /build/brook/Projects/cmake/03/build
[brook@:~/Projects/cmake/03/build]$ make
Scanning dependencies of target MyMathLib1
[ 25%] Building C object mymath01/CMakeFiles/MyMathLib1.dir/mymath01.c.o
[ 50%] Linking C static library libMyMathLib1.a
[ 50%] Built target MyMathLib1
Scanning dependencies of target Tutorial
[ 75%] Building C object CMakeFiles/Tutorial.dir/hello.c.o
/build/brook/Projects/cmake/03/src/hello.c:6:9: note: #pragma message: use mymath
 #pragma message("use mymath")
         ^~~~~~~
[100%] Linking C executable Tutorial
[100%] Built target Tutorial
[brook@:~/Projects/cmake/03/build]$ ./Tutorial 3
mymath: 9
[brook@:~/Projects/cmake/03/build]$ rm -rf *
[brook@:~/Projects/cmake/03/build]$ cmake ../src -DUSE_MYMATH=off
-- The C compiler identification is GNU 7.5.0
-- The CXX compiler identification is GNU 7.5.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /build/brook/Projects/cmake/03/build
[brook@:~/Projects/cmake/03/build]$ make
Scanning dependencies of target Tutorial
[ 50%] Building C object CMakeFiles/Tutorial.dir/hello.c.o
/build/brook/Projects/cmake/03/src/hello.c:9:9: note: #pragma message: use system
 #pragma message("use system")
         ^~~~~~~
[100%] Linking C executable Tutorial
[100%] Built target Tutorial
[brook@:~/Projects/cmake/03/build]$ ./Tutorial 3
system: 9




2022年6月25日 星期六

CMake - Step 2: Adding A Library As An Option


很多時候我們會在專案中決定使用哪個library, 這裡會舉個範例, 使用自訂的library或是使用系統的library,首先要在最上層的CMakeLists.txt中用option()定義我們的變數USE_MYMATH, on代表用自訂的library, 否則用系統的library, 所以相對應的target_link_libraries()與target_include_directories()也要根據USE_MYMATH稍微做一下調整, 所以我們用EXTRA_LIBS與EXTRA_INCLUDES分別去儲存對應的header file位置與相關的library資訊
# Require a minimum version of cmake.
# cmake_minimum_required(VERSION <min>>[...<policy_max>] [FATAL_ERROR])
cmake_minimum_required(VERSION 3.10)

# Set the name of the project.
# project(<PROJECT-NAME< [<anguage-name>...])
# project(<PROJECT-NAME>
#         [VERSION <major>[.<minor>[.<patch>[.<tweak>]]]]
#         [DESCRIPTION <project-description-string>]
#         [LANGUAGES <language-name>...])
project(Tutorial VERSION 0.1.2.3 DESCRIPTION "This is Brook 1st CMake Lib")

# Add an executable to the project using the specified source files.
# add_executable(<name> [WIN32] [MACOSX_BUNDLE]
#                [EXCLUDE_FROM_ALL]
#                [source1] [source2 ...])
add_executable(Tutorial hello.c)

# option(<variable> "<help_text>" [value])
option(USE_MYMATH "Use tutorial provided math implementation" ON)

# configure_file(<input> <output>
#               [NO_SOURCE_PERMISSIONS | USE_SOURCE_PERMISSIONS |
#                FILE_PERMISSIONS <permissions>...]
#               [COPYONLY] [ESCAPE_QUOTES] [@ONLY]
#               [NEWLINE_STYLE [UNIX|DOS|WIN32|LF|CRLF] ])
# configure a header file to pass some of the CMake settings
# to the source code
configure_file(Config.h.in Config.h)

if (USE_MYMATH)
# Add a subdirectory to the build.
# add_subdirectory(source_dir [binary_dir]
#                        [EXCLUDE_FROM_ALL])
add_subdirectory(mymath01)

# list(APPEND <list> [<element> ...])
# Appends elements to the list.
# If no variable named <list> exists in the current
# scope its value is treated as empty and the elements
# are appended to that empty list.
list(APPEND EXTRA_LIBS MyMathLib1)
list(APPEND EXTRA_INCLUDES "${PROJECT_SOURCE_DIR}/mymath01")
else()
list(APPEND EXTRA_LIBS "m")
endif()

# Specify libraries or flags to use when linking a given target and/or its dependents.
# target_link_libraries(<target>
#                      <PRIVATE|PUBLIC|INTERFACE> ...
#                      [<PRIVATE|PUBLIC|INTERFACE> ...]...)
target_link_libraries(Tutorial PUBLIC ${EXTRA_LIBS})

# Add include directories to a target.
# target_include_directories(&;lt;target> [SYSTEM] [AFTER|BEFORE]
#   &;lt;INTERFACE|PUBLIC|PRIVATE> [items1...]
#   [&;lt;INTERFACE|PUBLIC|PRIVATE> [items2...] ...])
target_include_directories(Tutorial PUBLIC "${PROJECT_BINARY_DIR}" ${EXTRA_INCLUDES})

接著會利用Config.h.in的cmakedefine去define USE_MYMATH, hello.c則需要include "Config.h"並且利用#ifdef/#else/#endif做一些對應的修改
[brook@:~/Projects/cmake/02-2]$ tree
.
`-- src
    |-- CMakeLists.txt
    |-- Config.h.in
    |-- hello.c
    `-- mymath01
        |-- CMakeLists.txt
        |-- mymath01.c
        `-- mymath01.h

2 directories, 6 files
[brook@:~/Projects/cmake/02-2]$ cat src/Config.h.in
#cmakedefine USE_MYMATH
[brook@:~/Projects/cmake/02-2]$ cat src/hello.c
#include <stdio.h>
#include <stdlib.h>
#include "Config.h"

#ifdef USE_MYMATH
#pragma message("use mymath")
#include "mymath01.h"
#else
#pragma message("use system")
#include 
#endif

int main(int argc, char *argv[])
{
#ifdef USE_MYMATH
        printf("mymath: %d\n", squar(atoi(argv[1])));
#else
        printf("system: %d\n", (int)pow(atoi(argv[1]), 2));
#endif
        return 0;
}

[brook@:~/Projects/cmake/02-2/build]$ cmake ../src
-- The C compiler identification is GNU 7.5.0
-- The CXX compiler identification is GNU 7.5.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /build/brook/Projects/cmake/02-2/build
[brook@:~/Projects/cmake/02-2/build]$ make
Scanning dependencies of target MyMathLib1
[ 25%] Building C object mymath01/CMakeFiles/MyMathLib1.dir/mymath01.c.o
[ 50%] Linking C static library libMyMathLib1.a
[ 50%] Built target MyMathLib1
Scanning dependencies of target Tutorial
[ 75%] Building C object CMakeFiles/Tutorial.dir/hello.c.o
/build/brook/Projects/cmake/02-2/src/hello.c:6:9: note: #pragma message: use mymath
 #pragma message("use mymath")
         ^~~~~~~
[100%] Linking C executable Tutorial
[100%] Built target Tutorial
[brook@:~/Projects/cmake/02-2/build]$ ./Tutorial 3
mymath: 9
[brook@:~/Projects/cmake/02-2/build]$ cmake ../src/ -DUSE_MYMATH=off
-- Configuring done
-- Generating done
-- Build files have been written to: /build/brook/Projects/cmake/02-2/build
[brook@:~/Projects/cmake/02-2/build]$ make
Scanning dependencies of target Tutorial
[ 50%] Building C object CMakeFiles/Tutorial.dir/hello.c.o
/build/brook/Projects/cmake/02-2/src/hello.c:9:9: note: #pragma message: use system
 #pragma message("use system")
         ^~~~~~~
[100%] Linking C executable Tutorial
[100%] Built target Tutorial
[brook@:~/Projects/cmake/02-2/build]$ ./Tutorial 3
system: 9





2022年6月19日 星期日

CMake - Step 2: Adding a Library


這篇主要要談如何建立一個library, 首先我們再src底下創建目錄mymath01用來放置我們的library, 裡面的soucre code包含mymath01.c與mymath01.h, 所以src/CMakeList.txt需要用add_subdirectory(mymath01)告訴CMake要編譯src/mymath01, 而src/mymath01/CMakeList.txt需要描述如何編譯Library, 主要是用add_library()告訴CMake這個library要用哪些source code編譯.
[brook@:~/Projects/cmake/02]$ tree
.
`-- src
    |-- CMakeLists.txt
    `-- mymath01
        |-- CMakeLists.txt
        |-- mymath01.c
        `-- mymath01.h

2 directories, 4 files
[brook@:~/Projects/cmake/02]$ cat src/CMakeLists.txt
# Require a minimum version of cmake.
# cmake_minimum_required(VERSION <min>>[...<policy_max>] [FATAL_ERROR])
cmake_minimum_required(VERSION 3.10)

# Set the name of the project.
# project(<PROJECT-NAME< [<anguage-name>...])
# project(<PROJECT-NAME>
#         [VERSION <major>[.<minor>[.<patch>[.<tweak>]]]]
#         [DESCRIPTION <project-description-string>]
#         [LANGUAGES <language-name>...])
project(Tutorial VERSION 0.1.2.3 DESCRIPTION "This is Brook 1st CMake Lib")

# Add a subdirectory to the build.
# add_subdirectory(source_dir [binary_dir]
#                        [EXCLUDE_FROM_ALL])
add_subdirectory(mymath01)


[brook@:~/Projects/cmake/02]$ cat src/mymath01/CMakeLists.txt
# Add a library to the project using the specified source files.
# add_library( [STATIC | SHARED | MODULE]
#             [EXCLUDE_FROM_ALL]
#                       source1 [source2 ...])
add_library(MyMathLib1 mymath01.c)

[brook@:~/Projects/cmake/02]$ cat src/mymath01/mymath01.h
#ifndef MYMATH01_H
#define MYMATH01_H

int squar(int a);

#endif
[brook@:~/Projects/cmake/02]$ cat src/mymath01/mymath01.c
#include "mymath01.h"

int squar(int a)
{
  return a*a;
}

[brook@:~/Projects/cmake/02]$ mkdir build && cd build
[brook@:~/Projects/cmake/02/build]$ cmake ../src/
-- The C compiler identification is GNU 7.5.0
-- The CXX compiler identification is GNU 7.5.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /build/brook/Projects/cmake/02/build
[brook@:~/Projects/cmake/02/build]$ cmake --build .
Scanning dependencies of target MyMathLib1
[ 50%] Building C object mymath01/CMakeFiles/MyMathLib1.dir/mymath01.c.o
[100%] Linking C static library libMyMathLib1.a
[100%] Built target MyMathLib1
[brook@:~/Projects/cmake/02/build]$ ls
CMakeCache.txt  CMakeFiles  Makefile  cmake_install.cmake  mymath01
[brook@:~/Projects/cmake/02/build]$ ls mymath01/
CMakeFiles  Makefile  cmake_install.cmake  libMyMathLib1.a
[brook@:~/Projects/cmake/02/build]$ nm mymath01/libMyMathLib1.a

mymath01.c.o:
0000000000000000 T squar

接著我們結合"Step 1"學的創建一個hello.c來引用MyMathLib1, 我們要用target_link_libraries()將MyMathLib1 link進來.
[brook@:~/Projects/cmake/02]$ tree
.
`-- src
    |-- CMakeLists.txt
    |-- hello.c
    `-- mymath01
        |-- CMakeLists.txt
        |-- mymath01.c
        `-- mymath01.h

2 directories, 5 files

[brook@:~/Projects/cmake/02]$ cat src/CMakeLists.txt
# Require a minimum version of cmake.
# cmake_minimum_required(VERSION <min>>[...<policy_max>] [FATAL_ERROR])
cmake_minimum_required(VERSION 3.10)

# Set the name of the project.
# project(<PROJECT-NAME< [<anguage-name>...])
# project(<PROJECT-NAME>
#         [VERSION <major>[.<minor>[.<patch>[.<tweak>]]]]
#         [DESCRIPTION <project-description-string>]
#         [LANGUAGES <language-name>...])
project(Tutorial VERSION 0.1.2.3 DESCRIPTION "This is Brook 1st CMake Lib")

# Add an executable to the project using the specified source files.
# add_executable(<name> [WIN32] [MACOSX_BUNDLE]
#                [EXCLUDE_FROM_ALL]
#                [source1] [source2 ...])
add_executable(Tutorial hello.c)

# Specify libraries or flags to use when linking a given target and/or its dependents.
# target_link_libraries(<target>
#                      <PRIVATE|PUBLIC|INTERFACE> ...
#                      [<PRIVATE|PUBLIC|INTERFACE> ...]...)
target_link_libraries(Tutorial PUBLIC MyMathLib1)

# Add include directories to a target.
# target_include_directories(&;lt;target> [SYSTEM] [AFTER|BEFORE]
#   &;lt;INTERFACE|PUBLIC|PRIVATE> [items1...]
#   [&;lt;INTERFACE|PUBLIC|PRIVATE> [items2...] ...])
target_include_directories(Tutorial PUBLIC "${PROJECT_SOURCE_DIR}/mymath01")

# Add a subdirectory to the build.
# add_subdirectory(source_dir [binary_dir]
#                        [EXCLUDE_FROM_ALL])
add_subdirectory(mymath01)

[brook@:~/Projects/cmake/02]$ cat src/hello.c
#include <stdio.h>
#include <stdlib.h>
#include "mymath01.h"

int main(int argc, char *argv[])
{
  printf("%d\n", squar(atoi(argv[1])));
  return 0;
}
[brook@:~/Projects/cmake/02]$ mkdir build && cd build
[brook@:~/Projects/cmake/02/build]$ cmake ../src
-- The C compiler identification is GNU 7.5.0
-- The CXX compiler identification is GNU 7.5.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /build/brook/Projects/cmake/02/build
[brook@:~/Projects/cmake/02/build]$ ls
CMakeCache.txt  CMakeFiles  Makefile  cmake_install.cmake  mymath01
[brook@:~/Projects/cmake/02/build]$ cmake --build .
Scanning dependencies of target MyMathLib1
[ 25%] Building C object mymath01/CMakeFiles/MyMathLib1.dir/mymath01.c.o
[ 50%] Linking C static library libMyMathLib1.a
[ 50%] Built target MyMathLib1
Scanning dependencies of target Tutorial
[ 75%] Building C object CMakeFiles/Tutorial.dir/hello.c.o
[100%] Linking C executable Tutorial
[100%] Built target Tutorial
[brook@:~/Projects/cmake/02/build]$ ls
CMakeCache.txt  CMakeFiles  Makefile  Tutorial  cmake_install.cmake  mymath01
[brook@:~/Projects/cmake/02/build]$ ./Tutorial  5
25

預設是build STATIC, 如果要改成SHARED, 只要加上SHARED這個Key word就可以了add_library(MyMathLib1 SHARED mymath01.c), 並利用set_target_properties()給shared library version與soversion.
[brook@:~/Projects/cmake/02-1]$ mkdir build && cd build
[brook@:~/Projects/cmake/02-1/build]$ cat ../src/mymath01/CMakeLists.txt
# Add a library to the project using the specified source files.
# add_library(<name> [STATIC | SHARED | MODULE]
#             [EXCLUDE_FROM_ALL]
#                       source1 [source2 ...])
add_library(MyMathLib1 SHARED mymath01.c)
set_target_properties(MyMathLib1 PROPERTIES VERSION 1.2.3 SOVERSION 1)

[brook@:~/Projects/cmake/02-1/build]$ cmake ../src
-- The C compiler identification is GNU 7.5.0
-- The CXX compiler identification is GNU 7.5.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /build/brook/Projects/cmake/02-1/build
[brook@:~/Projects/cmake/02-1/build]$ cmake --build .
Scanning dependencies of target MyMathLib1
[ 25%] Building C object mymath01/CMakeFiles/MyMathLib1.dir/mymath01.c.o
[ 50%] Linking C shared library libMyMathLib1.so
[ 50%] Built target MyMathLib1
Scanning dependencies of target Tutorial
[ 75%] Building C object CMakeFiles/Tutorial.dir/hello.c.o
[100%] Linking C executable Tutorial
[100%] Built target Tutorial
[brook@:~/Projects/cmake/02-1/build]$ ls -al mymath01/
total 32
drwxrwxrwx 3 brook chbg 4096 Jun 26 21:51 .
drwxrwxrwx 4 brook chbg 4096 Jun 26 21:51 ..
drwxrwxrwx 3 brook chbg 4096 Jun 26 21:51 CMakeFiles
-rw-rw-rw- 1 brook chbg 5602 Jun 26 21:50 Makefile
-rw-rw-rw- 1 brook chbg 1135 Jun 26 21:50 cmake_install.cmake
lrwxrwxrwx 1 brook chbg   18 Jun 26 21:51 libMyMathLib1.so -> libMyMathLib1.so.1
lrwxrwxrwx 1 brook chbg   22 Jun 26 21:51 libMyMathLib1.so.1 -> libMyMathLib1.so.1.2.3
-rwxrwxrwx 1 brook chbg 7440 Jun 26 21:51 libMyMathLib1.so.1.2.3





2022年6月11日 星期六

Table Of Content for "CMake Tutorial"



Step 1: A Basic Starting Point
Step 2: Adding a Library
- Step 2: Adding A Library As An Option
Step 3: Adding Usage Requirements for a Library
Step 4: Installing and Testing
Step 5: Adding System Introspection
Step 6: Adding a Custom Command and Generated File
Step 7: Packaging an Installer
Step 8: Adding Support for a Testing Dashboard
Step 9: Selecting Static or Shared Libraries
Step 10: Adding Generator Expressions
Step 11: Adding Export Configuration
Step 12: Packaging Debug and Release



2022年6月3日 星期五

CMake - Step 1: A Basic Starting Point


本文是CMake Tutorial的心得, CMake是一個用於build, test與package的cross-platform tool, 基本上是透過撰寫CMakeLists來描述如何build, test與package專案,接著會產生makefile, 再透過makefile產生最終的target(executable, library, or both)
讓我們從最簡單的CMakeLists.txt開始,基本上CMakeLists.txt的指令是不分大小寫的
# Require a minimum version of cmake.
# cmake_minimum_required(VERSION <min>>[...<policy_max>] [FATAL_ERROR])
cmake_minimum_required(VERSION 3.10)

# Set the name of the project.
# project(<PROJECT-NAME< [<anguage-name>...])
# project(<PROJECT-NAME>
#         [VERSION <major>[.<minor>[.<patch>[.<tweak>]]]]
#         [DESCRIPTION <project-description-string>]
#         [HOMEPAGE_URL <url-string>]
#         [LANGUAGES <language-name>...])
project(Tutorial VERSION 0.1.2 DESCRIPTION "This is Brook 1st CMake")

# Add an executable to the project using the specified source files.
# add_executable(<name> [WIN32] [MACOSX_BUNDLE]
#                [EXCLUDE_FROM_ALL]
#                [source1] [source2 ...])
add_executable(Tutorial hello.c)

hello.c
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
        printf("hello world\n");
        return 0;
}

有了CMakeLists.txt與source code, 就可以使用cmake [options] <path-to-source>建立Makefile, 並使用cmake --build <dir>去build targe
[brook@:~/Projects/cmake]$ tree
.
`-- src
    |-- CMakeLists.txt
    `-- hello.c

1 directory, 2 files
[brook@:~/Projects/cmake]$ mkdir build && cd build
[brook@:~/Projects/cmake/build]$ cmake ../src/
-- The C compiler identification is GNU 7.5.0
-- The CXX compiler identification is GNU 7.5.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /build/brook/Projects/cmake/build
[brook@:~/Projects/cmake/build]$ ls
CMakeCache.txt  CMakeFiles  Makefile  cmake_install.cmake
[brook@:~/Projects/cmake/build]$ cmake --build .
Scanning dependencies of target Tutorial
[ 50%] Building C object CMakeFiles/Tutorial.dir/hello.c.o
[100%] Linking C executable Tutorial
[100%] Built target Tutorial
[brook@:~/Projects/cmake/build]$ ls
CMakeCache.txt  CMakeFiles  Makefile  Tutorial  cmake_install.cmake
[brook@:~/Projects/cmake/build]$ ./Tutorial
hello world


Adding a Version Number and Configured Header File

前面的CMakeLists.txt中描述了project(Tutorial VERSION 0.1.2 DESCRIPTION "This is Brook 1st CMake"), 但是沒在C code中被用到, 所以, 我們將這些變數加入configure a header file 並傳遞給source code
# Require a minimum version of cmake.
# cmake_minimum_required(VERSION <min>>[...<policy_max>] [FATAL_ERROR])
cmake_minimum_required(VERSION 3.10)

# Set the name of the project.
# project(<PROJECT-NAME< [<anguage-name>...])
# project(<PROJECT-NAME>
#         [VERSION <major>[.<minor>[.<patch>[.<tweak>]]]]
#         [DESCRIPTION <project-description-string>]
#         [LANGUAGES <language-name>...])
project(Tutorial VERSION 0.1.2 DESCRIPTION "This is Brook 1st CMake")

# Add an executable to the project using the specified source files.
# add_executable(<name> [WIN32] [MACOSX_BUNDLE]
#                [EXCLUDE_FROM_ALL]
#                [source1] [source2 ...])
add_executable(Tutorial hello.c)


# Copy a file to another location and modify its contents.
# configure_file(<input> <output>
#                [NO_SOURCE_PERMISSIONS | USE_SOURCE_PERMISSIONS |
#                 FILE_PERMISSIONS <permissions>...]
#                [COPYONLY] [ESCAPE_QUOTES] [@ONLY]
#                [NEWLINE_STYLE [UNIX|DOS|WIN32|LF|CRLF] ])
configure_file(Config.h.in Config.h)

# Add include directories to a target.
# target_include_directories(&;lt;target> [SYSTEM] [AFTER|BEFORE]
#   &;lt;INTERFACE|PUBLIC|PRIVATE> [items1...]
#   [&;lt;INTERFACE|PUBLIC|PRIVATE> [items2...] ...])
target_include_directories(Tutorial PUBLIC "${PROJECT_BINARY_DIR}")


首先先透過configure_file()設定configure file的input與output, 在CMake建立Makefile過程中, 就會將configure的input變數取代後, 產生configure的output檔. 由於configure header file要被source code給include進來, 所以還得透過target_include_directories()設定對應的include path. 相關執行步驟如下:
[brook@:~/Projects/cmake]$ tree
.
`-- src
    |-- CMakeLists.txt
    |-- Config.h.in
    `-- hello.c

1 directory, 3 files
[brook@:~/Projects/cmake]$ cat src/Config.h.in
// the configured options and settings for Tutorial

#define VERSION_MAJOR @Tutorial_VERSION_MAJOR@
#define VERSION_MINOR @Tutorial_VERSION_MINOR@

[brook@:~/Projects/cmake]$ cat src/hello.c
#include <stdio.h>
#include <stdlib.h>
#include "Config.h"

int main(int argc, char *argv[])
{
        printf("hello world(version: %d.%d)\n", VERSION_MAJOR, VERSION_MINOR);
        return 0;
}

[brook@:~/Projects/cmake]$ mkdir build && cd build
[brook@:~/Projects/cmake/build]$ cmake ../src/
-- The C compiler identification is GNU 7.5.0
-- The CXX compiler identification is GNU 7.5.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /build/brook/Projects/cmake/build
[brook@:~/Projects/cmake/build]$ ls
CMakeCache.txt  CMakeFiles  Config.h  Makefile  cmake_install.cmake
[brook@:~/Projects/cmake/build]$ cmake --build .
Scanning dependencies of target Tutorial
[ 50%] Building C object CMakeFiles/Tutorial.dir/hello.c.o
[100%] Linking C executable Tutorial
[100%] Built target Tutorial
[brook@:~/Projects/cmake/build]$ ./Tutorial
hello world(version: 0.1)
[brook@:~/Projects/cmake/build]$ cat Config.h
// the configured options and settings for Tutorial
#define VERSION_MAJOR 0
#define VERSION_MINOR 1

在這裡我們有用到幾個CMake的變數, 包含PROJECT_BINARY_DIR, <PROJECT-NAME>_VERSION_MAJOR, PROJECT_BINARY_DIR等等, 這些都可以參考cmake-variables(7)取得更多的變數與相關細節, 我這裡就簡單介紹幾個基本的, 並透過Configure header file讓大家稍微理解一下對應的值
[brook@:~/Projects/cmake/build]$ cat ../src/Config.h.in
// the configured options and settings for Tutorial
#define VERSION_MAJOR @Tutorial_VERSION_MAJOR@
#define VERSION_MINOR @Tutorial_VERSION_MINOR@

// <PROJECT-NAME>_BINARY_DIR
// A variable is created with the name used in the project() command,
// and is the binary directory for the project.
#define Tutorial_BINARY_DIR @Tutorial_BINARY_DIR@

// <PROJECT-NAME>_SOURCE_DIR
// Top level source directory for the named project.
// A variable is created with the name used in the project() command,
// and is the source directory for the project.
#define Tutorial_SOURCE_DIR @Tutorial_SOURCE_DIR@

// <PROJECT-NAME>_VERSION
// Value given to the VERSION option of the most recent call
// to the project() command with project name <PROJECT-NAME>
#define Tutorial_VERSION "@Tutorial_VERSION@"
#define Tutorial_VERSION_MAJOR @Tutorial_VERSION_MAJOR@
#define Tutorial_VERSION_MINOR @Tutorial_VERSION_MINOR@
#define Tutorial_VERSION_PATCH @Tutorial_VERSION_PATCH@
#define Tutorial_VERSION_TWEAK @Tutorial_VERSION_TWEAK@

// Full path to build directory for project.
// This is the binary directory of the most recent project() command.
#define PROJECT_BINARY_DIR @PROJECT_BINARY_DIR@

// Short project description given to the project command.
// This is the description given to the most recent project() command.
#define PROJECT_DESCRIPTION "@PROJECT_DESCRIPTION@"

// Name of the project given to the project command.
// This is the name given to the most recent project() command.
#define PROJECT_NAME @PROJECT_NAME@

// Top level source directory for the current project.
// This is the source directory of the most recent project() command.
#define PROJECT_SOURCE_DIR @PROJECT_SOURCE_DIR@

// Value given to the VERSION option of the most recent call to the project() command
#define PROJECT_VERSION "@PROJECT_VERSION@"
#define PROJECT_VERSION_MAJOR @PROJECT_VERSION_MAJOR@
#define PROJECT_VERSION_MINOR @PROJECT_VERSION_MINOR@
#define PROJECT_VERSION_PATCH @PROJECT_VERSION_PATCH@
#define PROJECT_VERSION_TWEAK @PROJECT_VERSION_TWEAK@

[brook@:~/Projects/cmake/build]$ cmake ../src/
-- Configuring done
-- Generating done
-- Build files have been written to: /build/brook/Projects/cmake/build
[brook@:~/Projects/cmake/build]$ cat Config.h
// the configured options and settings for Tutorial
#define VERSION_MAJOR 0
#define VERSION_MINOR 1

// <PROJECT-NAME>_BINARY_DIR
// A variable is created with the name used in the project() command,
// and is the binary directory for the project.
#define Tutorial_BINARY_DIR /build/brook/Projects/cmake/build

// <PROJECT-NAME>_SOURCE_DIR
// Top level source directory for the named project.
// A variable is created with the name used in the project() command,
// and is the source directory for the project.
#define Tutorial_SOURCE_DIR /build/brook/Projects/cmake/src

// <PROJECT-NAME>_VERSION
// Value given to the VERSION option of the most recent call
// to the project() command with project name <PROJECT-NAME>
#define Tutorial_VERSION "0.1.2"
#define Tutorial_VERSION_MAJOR 0
#define Tutorial_VERSION_MINOR 1
#define Tutorial_VERSION_PATCH 2
#define Tutorial_VERSION_TWEAK

// Full path to build directory for project.
// This is the binary directory of the most recent project() command.
#define PROJECT_BINARY_DIR /build/brook/Projects/cmake/build

// Short project description given to the project command.
// This is the description given to the most recent project() command.
#define PROJECT_DESCRIPTION "This is Brook 1st CMake"

// Name of the project given to the project command.
// This is the name given to the most recent project() command.
#define PROJECT_NAME Tutorial

// Top level source directory for the current project.
// This is the source directory of the most recent project() command.
#define PROJECT_SOURCE_DIR /build/brook/Projects/cmake/src

// Value given to the VERSION option of the most recent call to the project() command
#define PROJECT_VERSION "0.1.2"
#define PROJECT_VERSION_MAJOR 0
#define PROJECT_VERSION_MINOR 1
#define PROJECT_VERSION_PATCH 2
#define PROJECT_VERSION_TWEAK




2022年5月7日 星期六

Linux Kernel(2.1)- MAJRO NUMBER RESERVED FOR DYNAMIC ASSIGNMENT


Documentation/admin-guide/devices.txt 文檔中描述了各個major number的用途, 而這文章的重點是dynamic的範圍從234~254與384~511
 234-254 char	RESERVED FOR DYNAMIC ASSIGNMENT
		Character devices that request a dynamic allocation of major number will
		take numbers starting from 254 and downward.

 384-511 char	RESERVED FOR DYNAMIC ASSIGNMENT
		Character devices that request a dynamic allocation of major
		number will take numbers starting from 511 and downward,
		once the 234-254 range is full.

相關的代碼如下:
__register_chrdev_region()
  |-> find_dynamic_major()
    |-> 254 ~ 234 or 511 ~ 384 有空的就拿來用

int alloc_chrdev_region(dev_t *dev, unsigned baseminor, unsigned count,
                        const char *name)
{
  struct char_device_struct *cd;
  cd = __register_chrdev_region(0, baseminor, count, name);
  if (IS_ERR(cd))
    return PTR_ERR(cd);
  *dev = MKDEV(cd->major, cd->baseminor);
  return 0;
}

static struct char_device_struct *
__register_chrdev_region(unsigned int major, unsigned int baseminor,
                           int minorct, const char *name)
{
  struct char_device_struct *cd, *curr, *prev = NULL;
  int ret;
  int i;

  if (major >= CHRDEV_MAJOR_MAX) {
    pr_err("CHRDEV \"%s\" major requested (%u) is greater than the maximum (%u)\n",
        name, major, CHRDEV_MAJOR_MAX-1);
    return ERR_PTR(-EINVAL);
  }

  if (minorct > MINORMASK + 1 - baseminor) {
    pr_err("CHRDEV \"%s\" minor range requested (%u-%u) is out of range of maximum range (%u-%u) for a single major\n",
      name, baseminor, baseminor + minorct - 1, 0, MINORMASK);
    return ERR_PTR(-EINVAL);
  }

    cd = kzalloc(sizeof(struct char_device_struct), GFP_KERNEL);
    if (cd == NULL)
        return ERR_PTR(-ENOMEM);

    mutex_lock(&chrdevs_lock);

    if (major == 0) {
        ret = find_dynamic_major();
        if (ret < 0) {
            pr_err("CHRDEV \"%s\" dynamic allocation region is full\n",
                 name);
            goto out;
        }
        major = ret;
    }

    ret = -EBUSY;
    i = major_to_index(major);
    for (curr = chrdevs[i]; curr; prev = curr, curr = curr->next) {
        if (curr->major < major)
            continue;

        if (curr->major > major)
            break;

        if (curr->baseminor + curr->minorct <= baseminor)
            continue;

        if (curr->baseminor >= baseminor + minorct)
            break;

        goto out;
    }

    cd->major = major;
    cd->baseminor = baseminor;
    cd->minorct = minorct;
    strlcpy(cd->name, name, sizeof(cd->name));

    if (!prev) {
        cd->next = curr;
        chrdevs[i] = cd;
    } else {
        cd->next = prev->next;
        prev->next = cd;
    }

    mutex_unlock(&chrdevs_lock);
    return cd;
out:
    mutex_unlock(&chrdevs_lock);
    kfree(cd);
    return ERR_PTR(ret);
}

/* fs/char_dev.c */
#define CHRDEV_MAJOR_MAX 512
/* Marks the bottom of the first segment of free char majors */
#define CHRDEV_MAJOR_DYN_END 234
/* Marks the top and bottom of the second segment of free char majors */
#define CHRDEV_MAJOR_DYN_EXT_START 511
#define CHRDEV_MAJOR_DYN_EXT_END 384

#define CHRDEV_MAJOR_HASH_SIZE 255
static struct char_device_struct {
    struct char_device_struct *next;
    unsigned int major;
    unsigned int baseminor;
    int minorct;
    char name[64];
    struct cdev *cdev;        /* will die */
} *chrdevs[CHRDEV_MAJOR_HASH_SIZE];


static int find_dynamic_major(void)
{
    int i;
    struct char_device_struct *cd;

              /* from 254 ~ 234 */
    for (i = ARRAY_SIZE(chrdevs)-1; i >= CHRDEV_MAJOR_DYN_END; i--) {
        if (chrdevs[i] == NULL)
            return i;
    }

                /* from 511 ~ 384 */
    for (i = CHRDEV_MAJOR_DYN_EXT_START;
       i >= CHRDEV_MAJOR_DYN_EXT_END; i--) {
        for (cd = chrdevs[major_to_index(i)]; cd; cd = cd->next)
            if (cd->major == i)
                break;

        if (cd == NULL)
            return i;
    }

    return -EBUSY;
}


2022年4月2日 星期六

run Cortex-A57 with kernel 5.4 on qemu


這篇文章只是拿來記錄compile Kernel for Cortex-A57, 用於研究PCIe Driver, 其餘的rootfs與busybox請參考附錄
[brook@:~/Projects/qemu/linux-virt]$ sudo apt-get install gcc-aarch64-linux-gnu #for ARM64
[brook@:~/Projects/qemu/linux-virt]$ export ARCH=arm64
[brook@:~/Projects/qemu/linux-virt]$ export CROSS_COMPILE=aarch64-linux-gnu-
[brook@:~/Projects/qemu/linux-virt]$ cp arch/arm64/configs/defconfig .config
[brook@:~/Projects/qemu/linux-virt]$ make olddefconfig
  HOSTCC  scripts/basic/fixdep
  HOSTCC  scripts/kconfig/conf.o
  HOSTCC  scripts/kconfig/confdata.o
  HOSTCC  scripts/kconfig/expr.o
  LEX     scripts/kconfig/lexer.lex.c
  YACC    scripts/kconfig/parser.tab.[ch]
  HOSTCC  scripts/kconfig/lexer.lex.o
  HOSTCC  scripts/kconfig/parser.tab.o
  HOSTCC  scripts/kconfig/preprocess.o
  HOSTCC  scripts/kconfig/symbol.o
  HOSTLD  scripts/kconfig/conf
scripts/kconfig/conf  --olddefconfig Kconfig
#
# configuration written to .config
#
[brook@:~/Projects/qemu/linux-virt]$ make -j16
[brook@:~/Projects/qemu/linux-virt]$ qemu-system-aarch64 -machine virt -cpu cortex-a57 -smp 8 -m 4096 -kernel  ./arch/arm64/boot/Image -append "console=ttyAMA0 root=/dev/vda" -nographic -initrd ../initrd-arm.img
[    0.000000] Booting Linux on physical CPU 0x0000000000 [0x411fd070]
...
Please press Enter to activate this console.
/ #
/ # lspci -k
00:01.0 Class 0200: 1af4:1000 virtio-pci
00:00.0 Class 0600: 1b36:0008
/ # uname -r
5.4.0
1af4:1000是Virtio network device, 而1b36:0008是QEMU PCIe Host bridge
ETH PCIe driver在"drivers/virtio/virtio_pci_common.c", 其vendor ID是0x1af4, 當device插入時, 就會去比對ID, match後就會載入該module並probe
/* Qumranet donated their vendor ID for devices 0x1000 thru 0x10FF. */
static const struct pci_device_id virtio_pci_id_table[] = {
        { PCI_DEVICE(PCI_VENDOR_ID_REDHAT_QUMRANET, PCI_ANY_ID) },
        { 0 }
};

MODULE_DEVICE_TABLE(pci, virtio_pci_id_table);
...
static struct pci_driver virtio_pci_driver = {
        .name           = "virtio-pci",
        .id_table       = virtio_pci_id_table,
        .probe          = virtio_pci_probe,
        .remove         = virtio_pci_remove,
#ifdef CONFIG_PM_SLEEP
        .driver.pm      = &virtio_pci_pm_ops,
#endif
        .sriov_configure = virtio_pci_sriov_configure,
};

module_pci_driver(virtio_pci_driver);

這裡我把PCI ID移成PCI_DEVICE(PCI_VENDOR_ID_REDHAT_QUMRANET + 1, 再透過echo <vendor_code> <device_code> > /sys/bus/pci/drivers/<pci_device_driver>/new_id動態對PCIe driver新增ID, 讓系統認到網卡
[brook@:~/Projects/qemu/linux-virt]$ git diff .
diff --git a/drivers/virtio/virtio_pci_common.c b/drivers/virtio/virtio_pci_common.c
index f2862f66c2ac..60aef3fea650 100644
--- a/drivers/virtio/virtio_pci_common.c
+++ b/drivers/virtio/virtio_pci_common.c
@@ -492,7 +492,7 @@ static const struct dev_pm_ops virtio_pci_pm_ops = {

 /* Qumranet donated their vendor ID for devices 0x1000 thru 0x10FF. */
 static const struct pci_device_id virtio_pci_id_table[] = {
-       { PCI_DEVICE(PCI_VENDOR_ID_REDHAT_QUMRANET, PCI_ANY_ID) },
+       { PCI_DEVICE(PCI_VENDOR_ID_REDHAT_QUMRANET + 1, PCI_ANY_ID) },
        { 0 }
 };

@@ -514,6 +514,7 @@ static int virtio_pci_probe(struct pci_dev *pci_dev,
 {
        struct virtio_pci_device *vp_dev, *reg_dev = NULL;
        int rc;
+       printk("%s(#%d): Brook\n", __FUNCTION__, __LINE__);

        /* allocate our structure and fill it out */
        vp_dev = kzalloc(sizeof(struct virtio_pci_device), GFP_KERNEL);
lspci會認到1af4:1000, 但是eth driver因為被我跳號, 所以認不到, 再透過/sys/bus/pci/drivers/<pci_device_driver>/new_id將往卡帶起來
/ # lspci
00:01.0 Class 0200: 1af4:1000
00:00.0 Class 0600: 1b36:0008
/ # ifconfig -a
lo        Link encap:Local Loopback
          LOOPBACK  MTU:65536  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)
/ # echo 1af4 1000 > /sys/bus/pci/drivers/virtio-pci/new_id
[  130.728345] virtio_pci_probe(#517): Brook
[  130.729216] virtio-pci 0000:00:01.0: enabling device (0000 -> 0003)
/ # ifconfig -a
eth0      Link encap:Ethernet  HWaddr 52:54:00:12:34:56
          BROADCAST MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

lo        Link encap:Local Loopback
          LOOPBACK  MTU:65536  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

透過echo <Domain:Bus:Device.Function> > /sys/bus/pci/drivers/<pci_device_driver>/unbind將driver移除, 也可以透過echo <Domain:Bus:Device.Function> > /sys/bus/pci/drivers/<pci_device_driver>/bind重新將driver帶上
/ # lspci
00:01.0 Class 0200: 1af4:1000
00:00.0 Class 0600: 1b36:0008
/ # echo 0000:00:01.0 > /sys/bus/pci/drivers/virtio-pci/unbind
[ 5163.097254] hrtimer: interrupt took 87350512 ns
/ # ifconfig -a
lo        Link encap:Local Loopback
          LOOPBACK  MTU:65536  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

/ # echo 0000:00:01.0 > /sys/bus/pci/drivers/virtio-pci/bind
[ 5183.251580] virtio_pci_probe(#517): Brook
/ # ifconfig -a
eth0      Link encap:Ethernet  HWaddr 52:54:00:12:34:56
          BROADCAST MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

lo        Link encap:Local Loopback
          LOOPBACK  MTU:65536  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)


    參考資料:
  • https://blog.csdn.net/zhqh100/article/details/51173275, qemu模拟Cortex-A57运行Linux4.5.1
  • Build the Linux Kernel and Busybox for ARM and run them on QEMU
  • https://zhuanlan.zhihu.com/p/113467453, qemu PCIe总线结构
  • https://pci-ids.ucw.cz/read/PC/1af4, The PCI ID Repository
  • https://stackoverflow.com/questions/22901282/hard-time-in-understanding-module-device-tableusb-id-table-usage, Hard time in understanding MODULE_DEVICE_TABLE(usb, id_table) usage




2022年3月26日 星期六

GDB - jump - 17.2 Continuing at a Different Address


jump顧名思義就是跳到某一行開始執行, 而且是會立刻執行直到遇到breakpoint. 因為jump並不會對stack, memory, 或register有任何改變(除了PC/program counter), 也因此jump的範圍需要在同一個function內部, 避免crash.
你也可以用set $pc=<execute_address> + "continue", "next", "step"替代
Type "apropos word" to search for commands related to "word"...
Reading symbols from a.out...done.
(gdb) set listsize unlimited
(gdb) list
1       #include <stdio.h>
2
3       static void possible_crash_1(void)
4       {
5           printf("%s(#%d)\n", __FUNCTION__, __LINE__);
6       }
7
8       static void possible_crash_2(void)
9       {
10          printf("%s(#%d)\n", __FUNCTION__, __LINE__);
11      }
12
13      static void possible_crash_3(void)
14      {
15          printf("%s(#%d)\n", __FUNCTION__, __LINE__);
16      }
17
18      int main(int argc, char *argv[])
19      {
20          possible_crash_1();
21
22          possible_crash_2();
23
24          possible_crash_3();
25
26          return 0;
27      }
28
(gdb) b main
Breakpoint 1 at 0x400576: file jump.c, line 20.
(gdb) run
Starting program: /build/brook/a.out

Breakpoint 1, main (argc=1, argv=0x7fffffffe4e8) at jump.c:20
20          possible_crash_1();
(gdb) j 22
Continuing at 0x40057b.
possible_crash_2(#10)
possible_crash_3(#15)
[Inferior 1 (process 9816) exited normally]
(gdb) run
Starting program: /build/brook/a.out

Breakpoint 1, main (argc=1, argv=0x7fffffffe4e8) at jump.c:20
20          possible_crash_1();
(gdb) set $pc=0x40057b
(gdb) n
possible_crash_2(#10)
24          possible_crash_3();
(gdb) n
possible_crash_3(#15)
26          return 0;



  • 參考資料:
      https://sourceware.org/gdb/download/onlinedocs/gdb/Jumping.html#Jumping, 17.2 Continuing at a Different Address



  • 2022年2月27日 星期日

    lighttpd & CGI note


    CGIC提供了簡單的API, 存取那一些CGIC已經幫你parse好的資料,比如: cgiRequestMethod, cgiRemoteAddr, cgiScriptName, 以及POST/GET資料等等, header的資料可以透過environ讀取
    可以透過cgiFormEntries(char ***result)抓key, 再透過cgiFormString(char *name, char *result, int max)取得資料,
    #include <stdio.h>
    #include <cgic.h>
    extern char **environ;
    
    int cgiMain() {
      char **array, **arrayStep, val[64];
      fprintf(cgiOut, "cgiRequestMethod:%s\n", cgiRequestMethod);
      fprintf(cgiOut, "cgiRemoteAddr:%s\n", cgiRemoteAddr);
      fprintf(cgiOut, "cgiScriptName:%s\n", cgiScriptName);
      fprintf(cgiOut, "cgiContentType:%s\n", cgiContentType);
      fprintf(cgiOut, "used for GET, cgiQueryString:%s\n", cgiQueryString);
    
      if (cgiFormEntries(&array) == cgiFormSuccess) {
          for (arrayStep = array; *arrayStep; arrayStep++) {
              fprintf(cgiOut, "get post by cgiFormEntries():%s\n", *arrayStep);
              cgiFormString(*arrayStep, val, sizeof(val));
              fprintf(cgiOut, "val:%s\n", val);
          }
      }
      for (array = environ; *array; array++) {
          fprintf(cgiOut, "get from env:%s\n", *array);
      }
    
      return 0;
    }
    


    GET

    [brook@:/var/www/html]$ curl -H "xhead: 123" --noproxy 127.0.0.1 http://127.0.0.1/cgic.cgi?xquery=123
    cgiRequestMethod:GET
    cgiRemoteAddr:127.0.0.1
    cgiScriptName:/cgic.cgi
    cgiContentType:
    used for GET, cgiQueryString:xquery=123
    get post by cgiFormEntries():xquery
    val:123
    get from env:CONTENT_LENGTH=0
    get from env:QUERY_STRING=xquery=123
    get from env:REQUEST_URI=/cgic.cgi?xquery=123
    get from env:REDIRECT_STATUS=200
    get from env:SCRIPT_NAME=/cgic.cgi
    get from env:SCRIPT_FILENAME=/var/www/html/cgic.cgi
    get from env:DOCUMENT_ROOT=/var/www/html
    get from env:REQUEST_METHOD=GET
    get from env:SERVER_PROTOCOL=HTTP/1.1
    get from env:SERVER_SOFTWARE=lighttpd/1.4.64
    get from env:GATEWAY_INTERFACE=CGI/1.1
    get from env:REQUEST_SCHEME=http
    get from env:SERVER_PORT=80
    get from env:SERVER_ADDR=127.0.0.1
    get from env:SERVER_NAME=127.0.0.1
    get from env:REMOTE_ADDR=127.0.0.1
    get from env:REMOTE_PORT=52894
    get from env:HTTP_HOST=127.0.0.1
    get from env:HTTP_USER_AGENT=curl/7.47.0
    get from env:HTTP_ACCEPT=*/*
    get from env:HTTP_XHEAD=123
    



    POST

    [brook@:/var/www/html]$ curl -H "xhead: 123" --noproxy 127.0.0.1 -X POST -d 'post1=p1&post2=p2' http://127.0.0.1/cgic.cgi?xquery=123
    cgiRequestMethod:POST
    cgiRemoteAddr:127.0.0.1
    cgiScriptName:/cgic.cgi
    cgiContentType:application/x-www-form-urlencoded
    used for GET, cgiQueryString:xquery=123
    get post by cgiFormEntries():post1
    val:p1
    get post by cgiFormEntries():post2
    val:p2
    get from env:CONTENT_LENGTH=17
    get from env:QUERY_STRING=xquery=123
    get from env:REQUEST_URI=/cgic.cgi?xquery=123
    get from env:REDIRECT_STATUS=200
    get from env:SCRIPT_NAME=/cgic.cgi
    get from env:SCRIPT_FILENAME=/var/www/html/cgic.cgi
    get from env:DOCUMENT_ROOT=/var/www/html
    get from env:REQUEST_METHOD=POST
    get from env:SERVER_PROTOCOL=HTTP/1.1
    get from env:SERVER_SOFTWARE=lighttpd/1.4.64
    get from env:GATEWAY_INTERFACE=CGI/1.1
    get from env:REQUEST_SCHEME=http
    get from env:SERVER_PORT=80
    get from env:SERVER_ADDR=127.0.0.1
    get from env:SERVER_NAME=127.0.0.1
    get from env:REMOTE_ADDR=127.0.0.1
    get from env:REMOTE_PORT=52904
    get from env:HTTP_HOST=127.0.0.1
    get from env:HTTP_USER_AGENT=curl/7.47.0
    get from env:HTTP_ACCEPT=*/*
    get from env:HTTP_XHEAD=123
    get from env:HTTP_CONTENT_LENGTH=17
    get from env:CONTENT_TYPE=application/x-www-form-urlencoded
    



    header的部分會被轉成HTTP_VAR=val方式存在environment中, 相關的code如下,
    http_cgi_encode_varname()
    {
      if (is_http_header) {
        memcpy(p, "HTTP_", 5);
        j = 5; /* "HTTP_" */
      }
      /* uppercase alpha */
      ...
    }
    
    http_cgi_headers()
    {
      ...
      for (n = 0; n < r->rqst_headers.used; n++) {
        http_cgi_encode_varname(tb, BUF_PTR_LEN(&ds->key), 1);
      }
      ...
    }
    
    cgi_create_env()
    {
      /* create environment */
      http_cgi_headers(r, &opts, cgi_env_add, env);
      ...
      pid_t pid = (dfd >= 0) ? fdevent_fork_execve(args[0], args, envp,
        to_cgi_fds[0], from_cgi_fds[1], serrh_fd, dfd): -1;
      ...
    }
    
    mod_cgi_handle_subrequest()
    {
      ...
      cgi_create_env();
      ...
    }
    


    熱門文章