sheepy

build system (sheepy) and package manager (spm) for C
git clone https://spartatek.se/git/sheepy.git
Log | Files | Refs | README | LICENSE

common.h (16365B)


      1 // MIT License
      2 //
      3 // Copyright (c) 2026 Remy Noulin
      4 //
      5 // Permission is hereby granted, free of charge, to any person obtaining a copy
      6 // of this software and associated documentation files (the "Software"), to deal
      7 // in the Software without restriction, including without limitation the rights
      8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
      9 // copies of the Software, and to permit persons to whom the Software is
     10 // furnished to do so, subject to the following conditions:
     11 //
     12 // The above copyright notice and this permission notice shall be included in all
     13 // copies or substantial portions of the Software.
     14 //
     15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
     18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
     20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
     21 // SOFTWARE.
     22 #ifndef _commonH
     23 #define _commonH
     24 #include "libsheepyObject.h"
     25 
     26 #define HOME_DIR    "~/.sheepy/"
     27 // HOME_DIR/build is the folder for building
     28 
     29 #define PACKAGE_DIR "shpPackages"
     30 
     31 #define CONFIG_NAME "config.yml"
     32 
     33 #define CONFIG_YML "---\n"\
     34                    "  tcc: false\n"\
     35                    "  print_compile_commands: false\n"\
     36                    "  clean_exe: true # when true sheepy -d deletes the executable or libraries\n"\
     37                    "  system: /usr/local # spm -g install the packages in this folder under "PACKAGE_DIR" and bin\n"\
     38                    "  cflags: -g3 -std=gnu11 -fPIC -pipe -Wno-unused-result # these flags are overriden by package.yml\n"\
     39                    "  lflags: -rdynamic # these flags are overriden by package.yml\n"\
     40                    "  linkWithGold: false\n"\
     41                    "  32bit: false\n"\
     42                    "  parallelSheepy: 1 # optional, 0 to disable, default is 1\n"\
     43                    "  parallelSpm: 1 # optional, 0 to disable, default is 1\n"\
     44                    "  #cflagsAlways: # these flags will always be present, independent of package.yml\n"\
     45                    "  # The test* are the options for building the test (sheepy -t, testBin in package.yml)\n"\
     46                    "  # gcov is enabled and the unit tests are done with check (apt-get install check, #include <check>)\n"\
     47                    "  testCflags: -g3 -std=gnu11 -fPIC -pipe -fprofile-arcs -ftest-coverage -Wall -Wextra # these flags are overriden by package.yml\n"\
     48                    "  testLflags: -lcheck_pic -lrt -lm -lsubunit -fprofile-arcs -ftest-coverage -rdynamic # these flags are overriden by package.yml\n"\
     49                    "  testLinkWithGold: false\n"\
     50                    "  #testCflagsAlways: # these flags will always be present, independent of package.yml\n"\
     51                    "  # The memcheck* are the options for building the memory leak tests (sheepy -m, memcheckBin in package.yml)\n"\
     52                    "  memcheckCmd: valgrind --leak-check=full --show-leak-kinds=all --error-exitcode=1\n"\
     53                    "  memcheckCflags: -g3 -std=gnu11 -fPIC -pipe # these flags are overriden by package.yml\n"\
     54                    "  memcheckLflags: -rdynamic # these flags are overriden by package.yml\n"\
     55                    "  memcheckLinkWithGold: false\n"\
     56                    "  # The asan* are the options for building the libsasan tests (sheepy -a, asanBin in package.yml)\n"\
     57                    "  asanCmd: env ASAN_OPTIONS=\"detect_leaks=1:detect_stack_use_after_return=1:halt_on_error=0:log_path=stdout:color=always:print_cmdline=1\"\n"\
     58                    "  asanCflags: -fsanitize=address -fno-omit-frame-pointer -fsanitize-recover=address # these flags are overriden by package.yml\n"\
     59                    "  asanLflags: -rdynamic -fsanitize=address -lasan # these flags are overriden by package.yml\n"\
     60                    "  asanLinkWithGold: false\n"\
     61                    "  debuggerCmd: gdb -tui --args\n"\
     62                    "  key: uninitialized"
     63 
     64 #define CONFIG_YML_NO_GOLD "---\n"\
     65                            "  tcc: false\n"\
     66                            "  print_compile_commands: false\n"\
     67                            "  clean_exe: true # when true sheepy -d deletes the executable or libraries\n"\
     68                            "  system: /usr/local # spm -g install the packages in this folder under "PACKAGE_DIR" and bin\n"\
     69                            "  cflags: -g3 -std=gnu11 -fPIC -pipe -Wno-unused-result # these flags are overriden by package.yml\n"\
     70                            "  lflags: -rdynamic # these flags are overriden by package.yml\n"\
     71                            "  linkWithGold: false\n"\
     72                            "  32bit: false\n"\
     73                            "  parallelSheepy: 1 # optional, 0 to disable, default is 1\n"\
     74                            "  parallelSpm: 1 # optional, 0 to disable, default is 1\n"\
     75                            "  #cflagsAlways: # these flags will always be present, independent of package.yml\n"\
     76                            "  # The test* are the options for building the test (sheepy -t, testBin in package.yml)\n"\
     77                            "  # gcov is enabled and the unit tests are done with check (apt-get install check, #include <check>)\n"\
     78                            "  testCflags: -g3 -std=gnu11 -fPIC -pipe -fprofile-arcs -ftest-coverage -Wall -Wextra # these flags are overriden by package.yml\n"\
     79                            "  testLflags: -lcheck_pic -lrt -lm -lsubunit -fprofile-arcs -ftest-coverage -rdynamic # these flags are overriden by package.yml\n"\
     80                            "  testLinkWithGold: false\n"\
     81                            "  #testCflagsAlways: # these flags will always be present, independent of package.yml\n"\
     82                            "  # The memcheck* are the options for building the memory leak tests (sheepy -m, memcheckBin in package.yml)\n"\
     83                            "  memcheckCmd: valgrind --leak-check=full --show-leak-kinds=all --error-exitcode=1\n"\
     84                            "  memcheckCflags: -g3 -std=gnu11 -fPIC -pipe # these flags are overriden by package.yml\n"\
     85                            "  memcheckLflags: -rdynamic # these flags are overriden by package.yml\n"\
     86                            "  memcheckLinkWithGold: false\n"\
     87                            "  # The asan* are the options for building the libsasan tests (sheepy -a, asanBin in package.yml)\n"\
     88                            "  asanCmd: env ASAN_OPTIONS=\"detect_leaks=1:detect_stack_use_after_return=1:halt_on_error=0:log_path=stdout:color=always:print_cmdline=1\"\n"\
     89                            "  asanCflags: -fsanitize=address -fno-omit-frame-pointer -fsanitize-recover=address # these flags are overriden by package.yml\n"\
     90                            "  asanLflags: -rdynamic -fsanitize=address -lasan # these flags are overriden by package.yml\n"\
     91                            "  asanLinkWithGold: false\n"\
     92                            "  debuggerCmd: gdb -tui --args\n"\
     93                            "  key: uninitialized"
     94 
     95 #define CONFIG_YML_NO_GOLD_ARM "---\n"\
     96                            "  tcc: false\n"\
     97                            "  print_compile_commands: false\n"\
     98                            "  clean_exe: true # when true sheepy -d deletes the executable or libraries\n"\
     99                            "  system: /data/data/com.termux/files/usr # spm -g install the packages in this folder under "PACKAGE_DIR" and bin\n"\
    100                            "  cflags: -D__arm__=1 -g3 -std=gnu11 -fPIC -pipe -Wno-unused-result # these flags are overriden by package.yml\n"\
    101                            "  lflags: -rdynamic # these flags are overriden by package.yml\n"\
    102                            "  linkWithGold: false\n"\
    103                            "  32bit: false\n"\
    104                            "  parallelSheepy: 1 # optional, 0 to disable, default is 1\n"\
    105                            "  parallelSpm: 1 # optional, 0 to disable, default is 1\n"\
    106                            "  #cflagsAlways: # these flags will always be present, independent of package.yml\n"\
    107                            "  # The test* are the options for building the test (sheepy -t, testBin in package.yml)\n"\
    108                            "  # gcov is enabled and the unit tests are done with check (apt-get install check, #include <check>)\n"\
    109                            "  testCflags: -g3 -std=gnu11 -fPIC -pipe -fprofile-arcs -ftest-coverage -Wall -Wextra # these flags are overriden by package.yml\n"\
    110                            "  testLflags: -lcheck_pic -lrt -lm -lsubunit -fprofile-arcs -ftest-coverage -rdynamic # these flags are overriden by package.yml\n"\
    111                            "  testLinkWithGold: false\n"\
    112                            "  #testCflagsAlways: # these flags will always be present, independent of package.yml\n"\
    113                            "  # The memcheck* are the options for building the memory leak tests (sheepy -m, memcheckBin in package.yml)\n"\
    114                            "  memcheckCmd: valgrind --leak-check=full --show-leak-kinds=all --error-exitcode=1\n"\
    115                            "  memcheckCflags: -g3 -std=gnu11 -fPIC -pipe # these flags are overriden by package.yml\n"\
    116                            "  memcheckLflags: -rdynamic # these flags are overriden by package.yml\n"\
    117                            "  memcheckLinkWithGold: false\n"\
    118                            "  # The asan* are the options for building the libsasan tests (sheepy -a, asanBin in package.yml)\n"\
    119                            "  asanCmd: env ASAN_OPTIONS=\"detect_leaks=1:detect_stack_use_after_return=1:halt_on_error=0:log_path=stdout:color=always:print_cmdline=1\"\n"\
    120                            "  asanCflags: -fsanitize=address -fno-omit-frame-pointer -fsanitize-recover=address # these flags are overriden by package.yml\n"\
    121                            "  asanLflags: -rdynamic -fsanitize=address -lasan # these flags are overriden by package.yml\n"\
    122                            "  asanLinkWithGold: false\n"\
    123                            "  debuggerCmd: gdb -tui --args\n"\
    124                            "  key: uninitialized"
    125 
    126 #define CONFIG_YML_HAIKU   "---\n"\
    127                            "  tcc: false\n"\
    128                            "  print_compile_commands: false\n"\
    129                            "  clean_exe: true # when true sheepy -d deletes the executable or libraries\n"\
    130                            "  system: /boot/home/config/non-packaged # spm -g install the packages in this folder under "PACKAGE_DIR" and bin\n"\
    131                            "  cflags: -g3 -std=gnu11 -fPIC -pipe -Wno-unused-result # these flags are overriden by package.yml\n"\
    132                            "  lflags: # these flags are overriden by package.yml\n"\
    133                            "  linkWithGold: false\n"\
    134                            "  32bit: false\n"\
    135                            "  parallelSheepy: 1 # optional, 0 to disable, default is 1\n"\
    136                            "  parallelSpm: 1 # optional, 0 to disable, default is 1\n"\
    137                            "  #cflagsAlways: # these flags will always be present, independent of package.yml\n"\
    138                            "  # The test* are the options for building the test (sheepy -t, testBin in package.yml)\n"\
    139                            "  # gcov is enabled and the unit tests are done with check (apt-get install check, #include <check>)\n"\
    140                            "  testCflags: -g3 -std=gnu11 -fPIC -pipe -fprofile-arcs -ftest-coverage -Wall -Wextra # these flags are overriden by package.yml\n"\
    141                            "  testLflags: -lcheck_pic -lrt -lm -lsubunit -fprofile-arcs -ftest-coverage -rdynamic # these flags are overriden by package.yml\n"\
    142                            "  testLinkWithGold: false\n"\
    143                            "  #testCflagsAlways: # these flags will always be present, independent of package.yml\n"\
    144                            "  # The memcheck* are the options for building the memory leak tests (sheepy -m, memcheckBin in package.yml)\n"\
    145                            "  memcheckCmd: valgrind --leak-check=full --show-leak-kinds=all --error-exitcode=1\n"\
    146                            "  memcheckCflags: -g3 -std=gnu11 -fPIC -pipe # these flags are overriden by package.yml\n"\
    147                            "  memcheckLflags: -rdynamic # these flags are overriden by package.yml\n"\
    148                            "  memcheckLinkWithGold: false\n"\
    149                            "  # The asan* are the options for building the libsasan tests (sheepy -a, asanBin in package.yml)\n"\
    150                            "  asanCmd: env ASAN_OPTIONS=\"detect_leaks=1:detect_stack_use_after_return=1:halt_on_error=0:log_path=stdout:color=always:print_cmdline=1\"\n"\
    151                            "  asanCflags: -fsanitize=address -fno-omit-frame-pointer -fsanitize-recover=address # these flags are overriden by package.yml\n"\
    152                            "  asanLflags: -rdynamic -fsanitize=address -lasan # these flags are overriden by package.yml\n"\
    153                            "  asanLinkWithGold: false\n"\
    154                            "  debuggerCmd: gdb -tui --args\n"\
    155                            "  key: uninitialized"
    156 
    157 extern char *defaultSpC;
    158 
    159 // package information
    160 #define PACKAGE "package.yml"
    161 
    162 
    163 #define C_TEMPLATE "#! /usr/bin/env sheepy\n\
    164 /* or direct path to sheepy: #! /usr/local/bin/sheepy */\n\
    165 \n\
    166 /* Libsheepy documentation: https://spartatek.se/libsheepy/ */\n\
    167 #include \"libsheepyObject.h\"\n\
    168 \n\
    169 int argc; char **argv;\n\
    170 \n\
    171 /* enable/disable logging */\n\
    172 /* #undef pLog */\n\
    173 /* #define pLog(...) */\n\
    174 \n\
    175 int main(int ARGC, char** ARGV) {\n\
    176 \n\
    177   argc = ARGC; argv = ARGV;\n\
    178 \n\
    179   initLibsheepy(ARGV[0]);\n\
    180   setLogMode(LOG_VERBOSE);\n\
    181   //pError0(openProgLogFile());\n\
    182   //setLogSymbols(LOG_UTF8);\n\
    183   //disableLibsheepyErrorLogs;\n\
    184 \n\
    185   logI(\"C template\");\n\
    186 \n\
    187 }\n\
    188 // vim: set expandtab ts=2 sw=2:"
    189 
    190 #define PKG_TEMPLATE "---\n\
    191   name: <PKG_NAME>\n\
    192   version: 0.0.1\n\
    193   description: \"explanation\"\n\
    194   bin: ./<PKG_NAME>.c\n\
    195   #cflags: -DA -g3 -std=gnu11 -fPIC -pipe\n\
    196   #lflags: -lpcre\n\
    197   repository:\n\
    198     type: git\n\
    199     url: git+https://github.com/USER/<PKG_NAME>.git\n\
    200   keywords:\n\
    201    #- utility\n\
    202     - command\n\
    203   author: Anonymous\n\
    204   license: MIT\n\
    205   bugs:\n\
    206     url: https://github.com/USER/<PKG_NAME>/issues\n\
    207   homepage: https://github.com/USER/<PKG_NAME>#readme\n\
    208   #compileHelp: # text displayed when there is a compilation error\n\
    209   #dependencies:\n\
    210   #  md4c: \n\
    211   # Test configuration:\n\
    212   #testBin: ./<TEST_NAME>.c\n\
    213   #testCflags: -g3 -std=gnu11 -fPIC -pipe -fprofile-arcs -ftest-coverage -Wall -Wextra\n\
    214   #testLflags: -lcheck_pic -lrt -lm -lsubunit -fprofile-arcs -ftest-coverage -rdynamic\n\
    215   # Memcheck configuration (sheepy -m):\n\
    216   #memcheckBin: ./<MEMCHECK_NAME>.c\n\
    217   #memcheckCmd: valgrind --leak-check=full --show-leak-kinds=all --error-exitcode=1\n\
    218   #memcheckCflags: -g3 -std=gnu11 -fPIC -pipe\n\
    219   #memcheckLflags: -rdynamic\n\
    220   # The asan* are the options for building the libsasan tests (sheepy -a):\n\
    221   #asanBin: ./<ASAN_NAME>.c\n\
    222   #asanCmd: env ASAN_OPTIONS=\"detect_leaks=1:detect_stack_use_after_return=1:halt_on_error=0:log_path=stdout:color=always:print_cmdline=1\"\n\
    223   #asanCflags: -fsanitize=address -fno-omit-frame-pointer -fsanitize-recover=address # these flags are overriden by package.yml\n\
    224   #asanLflags: -rdynamic -fsanitize=address -lasan # these flags are overriden by package.yml\n\
    225   #documentationCmd: # command for generating the documentation with spm doc\n\
    226   private: false # true for private package\
    227 "
    228 
    229 smallJsont *loadSheepyConfig(char **homedir);
    230 void sourceFromTemplate(const char *filename);
    231 void pkgFromTemplate(const char *pkgname);
    232 
    233 typedef void (*cbFt)(void *);
    234 void spawnProc(cbFt cb, void *arg);
    235 void waitForJobs(void);
    236 void childProc(void *arg);
    237 
    238 char* findLocalPackageDir(smallDictt* cfgD);
    239 
    240 typedef struct {
    241   pid_t pid;
    242   int status;
    243 } job;
    244 
    245 #define MAX_JOBS 10000
    246 staticArrayT(jobst, job, MAX_JOBS)
    247 extern jobst jobs;
    248 
    249 typedef struct {
    250   struct stat  st;
    251   /** source/header file path */
    252   char        *fpath;
    253   /** build path */
    254   char        *bpath;
    255 } srcFilet;
    256 
    257 #endif // #ifndef _commonH