| 1 |
/* |
| 2 |
* Copyright (c) 2014-2022 Joris Vink <[email protected]> |
| 3 |
* |
| 4 |
* Permission to use, copy, modify, and distribute this software for any |
| 5 |
* purpose with or without fee is hereby granted, provided that the above |
| 6 |
* copyright notice and this permission notice appear in all copies. |
| 7 |
* |
| 8 |
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 9 |
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 10 |
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
| 11 |
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 12 |
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 13 |
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 14 |
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 15 |
*/ |
| 16 |
|
| 17 |
#include <sys/param.h> |
| 18 |
#include <sys/types.h> |
| 19 |
#include <sys/stat.h> |
| 20 |
#include <sys/queue.h> |
| 21 |
#include <sys/wait.h> |
| 22 |
#include <sys/mman.h> |
| 23 |
#include <sys/time.h> |
| 24 |
|
| 25 |
#if !defined(KODEV_MINIMAL) |
| 26 |
#include <openssl/err.h> |
| 27 |
#include <openssl/pem.h> |
| 28 |
#include <openssl/x509v3.h> |
| 29 |
#endif |
| 30 |
|
| 31 |
#include <ctype.h> |
| 32 |
#include <errno.h> |
| 33 |
#include <dirent.h> |
| 34 |
#include <libgen.h> |
| 35 |
#include <inttypes.h> |
| 36 |
#include <fcntl.h> |
| 37 |
#include <time.h> |
| 38 |
#include <stdarg.h> |
| 39 |
#include <signal.h> |
| 40 |
#include <stdio.h> |
| 41 |
#include <stdlib.h> |
| 42 |
#include <string.h> |
| 43 |
#include <unistd.h> |
| 44 |
#include <utime.h> |
| 45 |
|
| •46 |
/* |
| 47 |
* Turn off deprecated function warnings when building against OpenSSL 3. |
| 48 |
* |
| 49 |
* The OpenSSL 3 library deprecated most low-level functions in favour |
| 50 |
* for their higher level APIs. |
| 51 |
* |
| 52 |
* I am planning a replacement, but for now we can still make it build |
| 53 |
* and function by ignoring these warnings completely. |
| 54 |
* |
| 55 |
* The functions in question are: |
| 56 |
* - SHA256_Init, SHA256_Update, SHA256_Final |
| 57 |
* - RSA_new, RSA_generate_key_ex |
| 58 |
* - EVP_PKEY_assign |
| 59 |
*/ |
| 60 |
#if defined(OPENSSL_VERSION_MAJOR) && OPENSSL_VERSION_MAJOR >= 3 |
| 61 |
#pragma GCC diagnostic ignored "-Wdeprecated-declarations" |
| 62 |
#endif |
| 63 |
|
| 64 |
#define errno_s strerror(errno) |
| 65 |
#define ssl_errno_s ERR_error_string(ERR_get_error(), NULL) |
| 66 |
|
| 67 |
#define LD_FLAGS_MAX 300 |
| 68 |
#define CFLAGS_MAX 300 |
| 69 |
#define CXXFLAGS_MAX CFLAGS_MAX |
| 70 |
|
| 71 |
#define BUILD_NOBUILD 0 |
| 72 |
#define BUILD_C 1 |
| 73 |
#define BUILD_CXX 2 |
| 74 |
|
| 75 |
struct cli_buf { |
| 76 |
u_int8_t *data; |
| 77 |
size_t length; |
| 78 |
size_t offset; |
| 79 |
}; |
| 80 |
|
| 81 |
struct mime_type { |
| 82 |
char *ext; |
| 83 |
char *type; |
| 84 |
TAILQ_ENTRY(mime_type) list; |
| 85 |
}; |
| 86 |
|
| 87 |
TAILQ_HEAD(mime_list, mime_type); |
| 88 |
|
| 89 |
struct buildopt { |
| 90 |
char *name; |
| 91 |
char *kore_source; |
| 92 |
char *kore_flavor; |
| 93 |
int flavor_nohttp; |
| 94 |
int single_binary; |
| 95 |
struct cli_buf *cflags; |
| 96 |
struct cli_buf *cxxflags; |
| 97 |
struct cli_buf *ldflags; |
| 98 |
TAILQ_ENTRY(buildopt) list; |
| 99 |
}; |
| 100 |
|
| 101 |
TAILQ_HEAD(buildopt_list, buildopt); |
| 102 |
|
| 103 |
struct cmd { |
| 104 |
const char *name; |
| 105 |
const char *descr; |
| 106 |
void (*cb)(int, char **); |
| 107 |
}; |
| 108 |
|
| 109 |
struct filegen { |
| 110 |
void (*cb)(void); |
| 111 |
}; |
| 112 |
|
| 113 |
struct cfile { |
| 114 |
struct stat st; |
| 115 |
int build; |
| 116 |
char *name; |
| 117 |
char *fpath; |
| 118 |
char *opath; |
| 119 |
TAILQ_ENTRY(cfile) list; |
| 120 |
}; |
| 121 |
|
| 122 |
TAILQ_HEAD(cfile_list, cfile); |
| 123 |
|
| 124 |
static struct cli_buf *cli_buf_alloc(size_t); |
| 125 |
static void cli_buf_free(struct cli_buf *); |
| 126 |
static char *cli_buf_stringify(struct cli_buf *, size_t *); |
| 127 |
static void cli_buf_append(struct cli_buf *, const void *, size_t); |
| 128 |
static void cli_buf_appendf(struct cli_buf *, const char *, ...) |
| 129 |
__attribute__((format (printf, 2, 3))); |
| 130 |
static void cli_buf_appendv(struct cli_buf *, const char *, |
| 131 |
va_list) __attribute__((format (printf, 2, 0))); |
| 132 |
|
| 133 |
static void *cli_malloc(size_t); |
| 134 |
static char *cli_strdup(const char *); |
| 135 |
static void *cli_realloc(void *, size_t); |
| 136 |
|
| 137 |
static char *cli_text_trim(char *, size_t); |
| 138 |
static char *cli_read_line(FILE *, char *, size_t); |
| 139 |
static long long cli_strtonum(const char *, long long, long long); |
| 140 |
static int cli_split_string(char *, const char *, char **, size_t); |
| 141 |
|
| 142 |
static void usage(void) __attribute__((noreturn)); |
| 143 |
static void fatal(const char *, ...) __attribute__((noreturn)) |
| 144 |
__attribute__((format (printf, 1, 2))); |
| 145 |
|
| 146 |
static void cli_file_close(int); |
| 147 |
static void cli_run_kore(void); |
| 148 |
static void cli_run_kore_python(void); |
| 149 |
static void cli_compile_kore(void *); |
| 150 |
static void cli_link_application(void *); |
| 151 |
static void cli_compile_source_file(void *); |
| 152 |
static void cli_mkdir(const char *, int); |
| 153 |
static int cli_dir_exists(const char *); |
| 154 |
static int cli_file_exists(const char *); |
| 155 |
static void cli_cleanup_files(const char *); |
| 156 |
static void cli_build_cflags(struct buildopt *); |
| 157 |
static void cli_build_cxxflags(struct buildopt *); |
| 158 |
static void cli_build_ldflags(struct buildopt *); |
| 159 |
static void cli_file_read(int, char **, size_t *); |
| 160 |
static void cli_file_writef(int, const char *, ...) |
| 161 |
__attribute__((format (printf, 2, 3))); |
| 162 |
static void cli_file_open(const char *, int, int *); |
| 163 |
static void cli_file_remove(char *, struct dirent *); |
| 164 |
static void cli_build_asset(char *, struct dirent *); |
| 165 |
static void cli_file_write(int, const void *, size_t); |
| 166 |
static int cli_vasprintf(char **, const char *, ...) |
| 167 |
__attribute__((format (printf, 2, 3))); |
| 168 |
static void cli_spawn_proc(void (*cb)(void *), void *); |
| 169 |
static void cli_write_asset(const char *, const char *, |
| 170 |
struct buildopt *); |
| 171 |
static void cli_register_kore_file(char *, struct dirent *); |
| 172 |
static void cli_register_source_file(char *, struct dirent *); |
| 173 |
static int cli_file_requires_build(struct stat *, const char *); |
| 174 |
static void cli_find_files(const char *, |
| 175 |
void (*cb)(char *, struct dirent *)); |
| 176 |
static void cli_add_source_file(char *, char *, char *, |
| 177 |
struct stat *, int); |
| 178 |
|
| 179 |
static struct buildopt *cli_buildopt_default(void); |
| 180 |
static struct buildopt *cli_buildopt_new(const char *); |
| 181 |
static struct buildopt *cli_buildopt_find(const char *); |
| 182 |
static void cli_buildopt_cleanup(void); |
| 183 |
static void cli_buildopt_parse(const char *); |
| 184 |
static void cli_buildopt_cflags(struct buildopt *, const char *); |
| 185 |
static void cli_buildopt_cxxflags(struct buildopt *, const char *); |
| 186 |
static void cli_buildopt_ldflags(struct buildopt *, const char *); |
| 187 |
static void cli_buildopt_single_binary(struct buildopt *, |
| 188 |
const char *); |
| 189 |
static void cli_buildopt_kore_source(struct buildopt *, |
| 190 |
const char *); |
| 191 |
static void cli_buildopt_kore_flavor(struct buildopt *, |
| 192 |
const char *); |
| 193 |
static void cli_buildopt_mime(struct buildopt *, const char *); |
| 194 |
|
| 195 |
static void cli_build_flags_common(struct buildopt *, |
| 196 |
struct cli_buf *); |
| 197 |
|
| 198 |
static void cli_flavor_load(void); |
| 199 |
static void cli_flavor_change(const char *); |
| 200 |
static void cli_kore_load_file(const char *, struct buildopt *, |
| 201 |
char **, size_t *); |
| 202 |
|
| 203 |
static void cli_run(int, char **); |
| 204 |
static void cli_help(int, char **); |
| 205 |
static void cli_info(int, char **); |
| 206 |
static void cli_build(int, char **); |
| 207 |
static void cli_clean(int, char **); |
| 208 |
static void cli_source(int, char **); |
| 209 |
static void cli_reload(int, char **); |
| 210 |
static void cli_flavor(int, char **); |
| 211 |
static void cli_cflags(int, char **); |
| 212 |
static void cli_ldflags(int, char **); |
| 213 |
static void cli_genasset(int, char **); |
| 214 |
static void cli_genasset_help(void); |
| 215 |
|
| 216 |
#if !defined(KODEV_MINIMAL) |
| 217 |
static void cli_create(int, char **); |
| 218 |
static void cli_create_help(void); |
| 219 |
|
| 220 |
static void file_create_src(void); |
| 221 |
static void file_create_config(void); |
| 222 |
static void file_create_gitignore(void); |
| 223 |
static void file_create_python_src(void); |
| 224 |
|
| 225 |
static void cli_generate_certs(void); |
| 226 |
static void cli_file_create(const char *, const char *, size_t); |
| 227 |
#endif |
| 228 |
|
| 229 |
static struct cmd cmds[] = { |
| 230 |
{ "help", "this help text", cli_help }, |
| 231 |
{ "run", "run an application (-fnr implied)", cli_run }, |
| 232 |
{ "gen", "generate asset file for compilation", cli_genasset }, |
| 233 |
{ "reload", "reload the application (SIGHUP)", cli_reload }, |
| 234 |
{ "info", "show info on kore on this system", cli_info }, |
| 235 |
{ "build", "build an application", cli_build }, |
| 236 |
{ "clean", "cleanup the build files", cli_clean }, |
| 237 |
{ "source", "print the path to kore sources", cli_source }, |
| 238 |
#if !defined(KODEV_MINIMAL) |
| 239 |
{ "create", "create a new application skeleton", cli_create }, |
| 240 |
#endif |
| 241 |
{ "flavor", "switch between build flavors", cli_flavor }, |
| 242 |
{ "cflags", "show kore CFLAGS", cli_cflags }, |
| 243 |
{ "ldflags", "show kore LDFLAGS", cli_ldflags }, |
| 244 |
{ NULL, NULL, NULL } |
| 245 |
}; |
| 246 |
|
| 247 |
#if !defined(KODEV_MINIMAL) |
| 248 |
static struct filegen gen_files[] = { |
| 249 |
{ file_create_src }, |
| 250 |
{ file_create_config }, |
| 251 |
{ file_create_gitignore }, |
| 252 |
{ NULL } |
| 253 |
}; |
| 254 |
|
| 255 |
static const char *gen_dirs[] = { |
| 256 |
"src", |
| 257 |
"cert", |
| 258 |
"conf", |
| 259 |
"assets", |
| 260 |
NULL |
| 261 |
}; |
| 262 |
|
| 263 |
static const char *python_gen_dirs[] = { |
| 264 |
"cert", |
| 265 |
NULL |
| 266 |
}; |
| 267 |
|
| 268 |
static struct filegen python_gen_files[] = { |
| 269 |
{ file_create_python_src }, |
| 270 |
{ file_create_gitignore }, |
| 271 |
{ NULL } |
| 272 |
}; |
| 273 |
|
| 274 |
static const char *http_serveable_function = |
| 275 |
"int\n" |
| 276 |
"asset_serve_%s_%s(struct http_request *req)\n" |
| 277 |
"{\n" |
| 278 |
" http_serveable(req, asset_%s_%s, asset_len_%s_%s,\n" |
| 279 |
" asset_sha256_%s_%s, \"%s\");\n" |
| 280 |
" return (KORE_RESULT_OK);\n" |
| 281 |
"}\n"; |
| 282 |
|
| 283 |
static const char *src_data = |
| 284 |
"#include <kore/kore.h>\n" |
| 285 |
"#include <kore/http.h>\n" |
| 286 |
"\n" |
| 287 |
"int\t\tpage(struct http_request *);\n" |
| 288 |
"\n" |
| 289 |
"int\n" |
| 290 |
"page(struct http_request *req)\n" |
| 291 |
"{\n" |
| 292 |
"\thttp_response(req, 200, NULL, 0);\n" |
| 293 |
"\treturn (KORE_RESULT_OK);\n" |
| 294 |
"}\n"; |
| 295 |
|
| 296 |
static const char *config_data = |
| 297 |
"# %s configuration\n" |
| 298 |
"\n" |
| 299 |
"server tls {\n" |
| 300 |
"\tbind 127.0.0.1 8888\n" |
| 301 |
"}\n" |
| 302 |
"\n" |
| 303 |
"load\t\t./%s.so\n" |
| 304 |
"\n" |
| 305 |
"domain * {\n" |
| 306 |
"\tattach\t\ttls\n" |
| 307 |
"\n" |
| 308 |
"\tcertfile\tcert/server.pem\n" |
| 309 |
"\tcertkey\t\tcert/key.pem\n" |
| 310 |
"\n" |
| 311 |
"\troute / {\n" |
| 312 |
"\t\thandler page\n" |
| 313 |
"\t}\n" |
| 314 |
"\n" |
| 315 |
"}\n"; |
| 316 |
|
| 317 |
static const char *build_data = |
| 318 |
"# %s build config\n" |
| 319 |
"# You can switch flavors using: kodev flavor [newflavor]\n" |
| 320 |
"\n" |
| 321 |
"# Set to yes if you wish to produce a single binary instead\n" |
| 322 |
"# of a dynamic library. If you set this to yes you must also\n" |
| 323 |
"# set kore_source together with kore_flavor.\n" |
| 324 |
"#single_binary=no\n" |
| 325 |
"#kore_source=/home/joris/src/kore\n" |
| 326 |
"#kore_flavor=\n" |
| 327 |
"\n" |
| 328 |
"# The flags below are shared between flavors\n" |
| 329 |
"cflags=-Wall -Wmissing-declarations -Wshadow\n" |
| 330 |
"cflags=-Wstrict-prototypes -Wmissing-prototypes\n" |
| 331 |
"cflags=-Wpointer-arith -Wcast-qual -Wsign-compare\n" |
| 332 |
"\n" |
| 333 |
"cxxflags=-Wall -Wmissing-declarations -Wshadow\n" |
| 334 |
"cxxflags=-Wpointer-arith -Wcast-qual -Wsign-compare\n" |
| 335 |
"\n" |
| 336 |
"# Mime types for assets served via the builtin asset_serve_*\n" |
| 337 |
"#mime_add=txt:text/plain; charset=utf-8\n" |
| 338 |
"#mime_add=png:image/png\n" |
| 339 |
"#mime_add=html:text/html; charset=utf-8\n" |
| 340 |
"\n" |
| 341 |
"dev {\n" |
| 342 |
" # These flags are added to the shared ones when\n" |
| 343 |
" # you build the \"dev\" flavor.\n" |
| 344 |
" cflags=-g\n" |
| 345 |
" cxxflags=-g\n" |
| 346 |
"}\n" |
| 347 |
"\n" |
| 348 |
"#prod {\n" |
| 349 |
"# You can specify additional flags here which are only\n" |
| 350 |
"# included if you build with the \"prod\" flavor.\n" |
| 351 |
"#}\n"; |
| 352 |
|
| 353 |
static const char *python_init_data = |
| 354 |
"from .app import koreapp\n"; |
| 355 |
|
| 356 |
static const char *python_app_data = |
| 357 |
"import kore\n" |
| 358 |
"\n" |
| 359 |
"class KoreApp:\n" |
| 360 |
" def configure(self, args):\n" |
| 361 |
" kore.config.deployment = \"development\"\n" |
| 362 |
" kore.server(\"default\", ip=\"127.0.0.1\", port=\"8888\")\n" |
| 363 |
"\n" |
| 364 |
" d = kore.domain(\"*\",\n" |
| 365 |
" attach=\"default\",\n" |
| 366 |
" key=\"cert/key.pem\",\n" |
| 367 |
" cert=\"cert/server.pem\",\n" |
| 368 |
" )\n" |
| 369 |
"\n" |
| 370 |
" d.route(\"/\", self.index, methods=[\"get\"])\n" |
| 371 |
"\n" |
| 372 |
" async def index(self, req):\n" |
| 373 |
" req.response(200, b'')\n" |
| 374 |
"\n" |
| 375 |
"koreapp = KoreApp()"; |
| 376 |
|
| 377 |
static const char *gitignore = "*.o\n.flavor\n.objs\n%s.so\nassets.h\ncert\n"; |
| 378 |
|
| 379 |
#endif /* !KODEV_MINIMAL */ |
| 380 |
|
| 381 |
static int s_fd = -1; |
| 382 |
static char *appl = NULL; |
| 383 |
static int run_after = 0; |
| 384 |
static char *compiler_c = "cc"; |
| 385 |
static char *compiler_cpp = "c++"; |
| 386 |
static char *compiler_ld = "cc"; |
| 387 |
static const char *prefix = PREFIX; |
| 388 |
static struct mime_list mime_types; |
| 389 |
static struct cfile_list source_files; |
| 390 |
static struct buildopt_list build_options; |
| 391 |
static int source_files_count; |
| 392 |
static int cxx_files_count; |
| 393 |
static struct cmd *command = NULL; |
| 394 |
static int cflags_count = 0; |
| 395 |
static int genasset_cmd = 0; |
| 396 |
static int cxxflags_count = 0; |
| 397 |
static int ldflags_count = 0; |
| 398 |
static char *flavor = NULL; |
| 399 |
static char *out_dir = "."; |
| 400 |
static char *object_dir = ".objs"; |
| 401 |
static char *cflags[CFLAGS_MAX]; |
| 402 |
static char *cxxflags[CXXFLAGS_MAX]; |
| 403 |
static char *ldflags[LD_FLAGS_MAX]; |
| 404 |
|
| 405 |
static void |
| 406 |
usage(void) |
| 407 |
{ |
| 408 |
int i; |
| 409 |
|
| 410 |
fprintf(stderr, "Usage: kodev [command]\n"); |
| 411 |
#if defined(KODEV_MINIMAL) |
| 412 |
fprintf(stderr, "minimal (only build commands supported)\n"); |
| 413 |
#endif |
| 414 |
fprintf(stderr, "\nAvailable commands:\n"); |
| 415 |
|
| 416 |
for (i = 0; cmds[i].name != NULL; i++) |
| 417 |
printf("\t%s\t%s\n", cmds[i].name, cmds[i].descr); |
| 418 |
|
| 419 |
fprintf(stderr, "\nFind more information on https://kore.io\n"); |
| 420 |
exit(1); |
| 421 |
} |
| 422 |
|
| 423 |
int |
| 424 |
main(int argc, char **argv) |
| 425 |
{ |
| 426 |
int i; |
| 427 |
char *env; |
| 428 |
|
| 429 |
if (argc < 2) |
| 430 |
usage(); |
| 431 |
|
| 432 |
argc--; |
| 433 |
argv++; |
| 434 |
|
| 435 |
if ((env = getenv("KORE_PREFIX")) != NULL) |
| 436 |
prefix = env; |
| 437 |
|
| 438 |
if ((env = getenv("KORE_OBJDIR")) != NULL) |
| 439 |
object_dir = env; |
| 440 |
|
| 441 |
if ((env = getenv("KODEV_OUTPUT")) != NULL) |
| 442 |
out_dir = env; |
| 443 |
|
| 444 |
(void)umask(S_IWGRP | S_IWOTH); |
| 445 |
|
| 446 |
for (i = 0; cmds[i].name != NULL; i++) { |
| 447 |
if (!strcmp(argv[0], cmds[i].name)) { |
| 448 |
if (strcmp(argv[0], "create")) { |
| 449 |
argc--; |
| 450 |
argv++; |
| 451 |
} |
| 452 |
command = &cmds[i]; |
| 453 |
cmds[i].cb(argc, argv); |
| 454 |
break; |
| 455 |
} |
| 456 |
} |
| 457 |
|
| 458 |
if (cmds[i].name == NULL) { |
| 459 |
fprintf(stderr, "unknown command: %s\n", argv[0]); |
| 460 |
usage(); |
| 461 |
} |
| 462 |
|
| 463 |
return (0); |
| 464 |
} |
| 465 |
|
| 466 |
static void |
| 467 |
cli_help(int argc, char **argv) |
| 468 |
{ |
| 469 |
usage(); |
| 470 |
} |
| 471 |
|
| 472 |
#if !defined(KODEV_MINIMAL) |
| 473 |
static void |
| 474 |
cli_create_help(void) |
| 475 |
{ |
| 476 |
printf("Usage: kodev create [-p] [name]\n"); |
| 477 |
printf("Synopsis:\n"); |
| 478 |
printf(" Create a new application skeleton directory structure.\n"); |
| 479 |
printf("\n"); |
| 480 |
printf(" Optional flags:\n"); |
| 481 |
printf("\t-p = generate a python application skeleton\n"); |
| 482 |
|
| 483 |
exit(1); |
| 484 |
} |
| 485 |
|
| 486 |
static void |
| 487 |
cli_create(int argc, char **argv) |
| 488 |
{ |
| 489 |
char *fpath; |
| 490 |
const char **dirs; |
| 491 |
struct filegen *files; |
| 492 |
int i, ch, python; |
| 493 |
|
| 494 |
python = 0; |
| 495 |
|
| 496 |
while ((ch = getopt(argc, argv, "hp")) != -1) { |
| 497 |
switch (ch) { |
| 498 |
case 'h': |
| 499 |
cli_create_help(); |
| 500 |
break; |
| 501 |
case 'p': |
| 502 |
python = 1; |
| 503 |
break; |
| 504 |
default: |
| 505 |
cli_create_help(); |
| 506 |
break; |
| 507 |
} |
| 508 |
} |
| 509 |
|
| 510 |
argc -= optind; |
| 511 |
argv += optind; |
| 512 |
|
| 513 |
if (argc != 1) |
| 514 |
cli_create_help(); |
| 515 |
|
| 516 |
appl = argv[0]; |
| 517 |
cli_mkdir(appl, 0755); |
| 518 |
|
| 519 |
if (python) { |
| 520 |
dirs = python_gen_dirs; |
| 521 |
files = python_gen_files; |
| 522 |
} else { |
| 523 |
dirs = gen_dirs; |
| 524 |
files = gen_files; |
| 525 |
} |
| 526 |
|
| 527 |
for (i = 0; dirs[i] != NULL; i++) { |
| 528 |
(void)cli_vasprintf(&fpath, "%s/%s", appl, dirs[i]); |
| 529 |
cli_mkdir(fpath, 0755); |
| 530 |
free(fpath); |
| 531 |
} |
| 532 |
|
| 533 |
for (i = 0; files[i].cb != NULL; i++) |
| 534 |
files[i].cb(); |
| 535 |
|
| 536 |
if (chdir(appl) == -1) |
| 537 |
fatal("chdir(%s): %s", appl, errno_s); |
| 538 |
|
| 539 |
cli_generate_certs(); |
| 540 |
|
| 541 |
printf("%s created successfully!\n", appl); |
| 542 |
printf("WARNING: DO NOT USE THE GENERATED CERTIFICATE IN PRODUCTION\n"); |
| 543 |
} |
| 544 |
#endif |
| 545 |
|
| 546 |
static void |
| 547 |
cli_flavor(int argc, char **argv) |
| 548 |
{ |
| 549 |
struct buildopt *bopt; |
| 550 |
char pwd[MAXPATHLEN], *conf; |
| 551 |
|
| 552 |
if (getcwd(pwd, sizeof(pwd)) == NULL) |
| 553 |
fatal("could not get cwd: %s", errno_s); |
| 554 |
|
| 555 |
appl = basename(pwd); |
| 556 |
(void)cli_vasprintf(&conf, "conf/%s.conf", appl); |
| 557 |
if (!cli_dir_exists("conf") || !cli_file_exists(conf)) |
| 558 |
fatal("%s doesn't appear to be a kore app", appl); |
| 559 |
free(conf); |
| 560 |
|
| 561 |
TAILQ_INIT(&build_options); |
| 562 |
TAILQ_INIT(&mime_types); |
| 563 |
(void)cli_buildopt_new("_default"); |
| 564 |
cli_buildopt_parse("conf/build.conf"); |
| 565 |
|
| 566 |
if (argc == 0) { |
| 567 |
cli_flavor_load(); |
| 568 |
TAILQ_FOREACH(bopt, &build_options, list) { |
| 569 |
if (!strcmp(bopt->name, "_default")) |
| 570 |
continue; |
| 571 |
if (!strcmp(bopt->name, flavor)) { |
| 572 |
printf("* %s\n", bopt->name); |
| 573 |
} else { |
| 574 |
printf(" %s\n", bopt->name); |
| 575 |
} |
| 576 |
} |
| 577 |
} else { |
| 578 |
cli_flavor_change(argv[0]); |
| 579 |
printf("changed build flavor to: %s\n", argv[0]); |
| 580 |
} |
| 581 |
|
| 582 |
cli_buildopt_cleanup(); |
| 583 |
} |
| 584 |
|
| 585 |
static void |
| 586 |
cli_build(int argc, char **argv) |
| 587 |
{ |
| 588 |
#if !defined(KODEV_MINIMAL) |
| 589 |
int l; |
| 590 |
char *data; |
| 591 |
#endif |
| 592 |
struct dirent dp; |
| 593 |
struct cfile *cf; |
| 594 |
struct buildopt *bopt; |
| 595 |
struct timeval times[2]; |
| 596 |
char *build_path; |
| 597 |
char *vsrc, *vobj; |
| 598 |
int requires_relink; |
| 599 |
char *sofile, *config; |
| 600 |
char *assets_path, *p, *src_path; |
| 601 |
char pwd[PATH_MAX], *assets_header; |
| 602 |
|
| 603 |
if (getcwd(pwd, sizeof(pwd)) == NULL) |
| 604 |
fatal("could not get cwd: %s", errno_s); |
| 605 |
|
| 606 |
appl = cli_strdup(basename(pwd)); |
| 607 |
|
| 608 |
if ((p = getenv("CC")) != NULL) { |
| 609 |
compiler_c = p; |
| 610 |
compiler_ld = p; |
| 611 |
} |
| 612 |
|
| 613 |
if ((p = getenv("CXX")) != NULL) { |
| 614 |
compiler_cpp = p; |
| 615 |
compiler_ld = p; |
| 616 |
} |
| 617 |
|
| 618 |
source_files_count = 0; |
| 619 |
cxx_files_count = 0; |
| 620 |
TAILQ_INIT(&source_files); |
| 621 |
TAILQ_INIT(&build_options); |
| 622 |
TAILQ_INIT(&mime_types); |
| 623 |
|
| 624 |
(void)cli_vasprintf(&src_path, "src"); |
| 625 |
(void)cli_vasprintf(&assets_path, "assets"); |
| 626 |
(void)cli_vasprintf(&config, "conf/%s.conf", appl); |
| 627 |
(void)cli_vasprintf(&build_path, "conf/build.conf"); |
| 628 |
(void)cli_vasprintf(&assets_header, "%s/assets.h", object_dir); |
| 629 |
|
| 630 |
if (!cli_dir_exists(src_path) || !cli_file_exists(config)) |
| 631 |
fatal("%s doesn't appear to be a kore app", appl); |
| 632 |
|
| 633 |
cli_flavor_load(); |
| 634 |
bopt = cli_buildopt_new("_default"); |
| 635 |
|
| 636 |
#if !defined(KODEV_MINIMAL) |
| 637 |
if (!cli_file_exists(build_path)) { |
| 638 |
l = cli_vasprintf(&data, build_data, appl); |
| 639 |
cli_file_create("conf/build.conf", data, l); |
| 640 |
free(data); |
| 641 |
} |
| 642 |
#endif |
| 643 |
|
| 644 |
cli_find_files(src_path, cli_register_source_file); |
| 645 |
free(src_path); |
| 646 |
|
| 647 |
cli_buildopt_parse(build_path); |
| 648 |
free(build_path); |
| 649 |
|
| 650 |
if (!cli_dir_exists(object_dir)) |
| 651 |
cli_mkdir(object_dir, 0755); |
| 652 |
|
| 653 |
if (bopt->single_binary) { |
| 654 |
if (bopt->kore_source == NULL) |
| 655 |
fatal("single_binary set but not kore_source"); |
| 656 |
|
| 657 |
printf("building kore (%s)\n", bopt->kore_source); |
| 658 |
cli_spawn_proc(cli_compile_kore, bopt); |
| 659 |
|
| 660 |
(void)cli_vasprintf(&src_path, "%s/src", bopt->kore_source); |
| 661 |
cli_find_files(src_path, cli_register_kore_file); |
| 662 |
free(src_path); |
| 663 |
|
| 664 |
(void)cli_vasprintf(&vsrc, "%s/version.c", object_dir); |
| 665 |
(void)cli_vasprintf(&vobj, "%s/version.o", object_dir); |
| 666 |
|
| 667 |
cli_add_source_file("version.c", |
| 668 |
vsrc, vobj, NULL, BUILD_NOBUILD); |
| 669 |
} |
| 670 |
|
| 671 |
printf("building %s (%s)\n", appl, flavor); |
| 672 |
|
| 673 |
cli_build_cflags(bopt); |
| 674 |
cli_build_cxxflags(bopt); |
| 675 |
cli_build_ldflags(bopt); |
| 676 |
|
| 677 |
(void)unlink(assets_header); |
| 678 |
|
| 679 |
/* Generate the assets. */ |
| 680 |
cli_file_open(assets_header, O_CREAT | O_TRUNC | O_WRONLY, &s_fd); |
| 681 |
cli_file_writef(s_fd, "#ifndef __H_KORE_ASSETS_H\n"); |
| 682 |
cli_file_writef(s_fd, "#define __H_KORE_ASSETS_H\n"); |
| 683 |
|
| 684 |
if (cli_dir_exists(assets_path)) |
| 685 |
cli_find_files(assets_path, cli_build_asset); |
| 686 |
|
| 687 |
if (bopt->single_binary) { |
| 688 |
memset(&dp, 0, sizeof(dp)); |
| 689 |
dp.d_type = DT_REG; |
| 690 |
printf("adding config %s\n", config); |
| 691 |
(void)snprintf(dp.d_name, |
| 692 |
sizeof(dp.d_name), "builtin_kore.conf"); |
| 693 |
cli_build_asset(config, &dp); |
| 694 |
} |
| 695 |
|
| 696 |
cli_file_writef(s_fd, "\n#endif\n"); |
| 697 |
cli_file_close(s_fd); |
| 698 |
|
| 699 |
free(assets_path); |
| 700 |
free(config); |
| 701 |
|
| 702 |
if (cxx_files_count > 0) |
| 703 |
compiler_ld = compiler_cpp; |
| 704 |
|
| 705 |
requires_relink = 0; |
| 706 |
TAILQ_FOREACH(cf, &source_files, list) { |
| 707 |
if (cf->build == BUILD_NOBUILD) |
| 708 |
continue; |
| 709 |
|
| 710 |
printf("compiling %s\n", cf->name); |
| 711 |
cli_spawn_proc(cli_compile_source_file, cf); |
| 712 |
|
| 713 |
times[0].tv_usec = 0; |
| 714 |
times[0].tv_sec = cf->st.st_mtime; |
| 715 |
times[1] = times[0]; |
| 716 |
|
| 717 |
if (utimes(cf->opath, times) == -1) |
| 718 |
printf("utime(%s): %s\n", cf->opath, errno_s); |
| 719 |
|
| 720 |
requires_relink++; |
| 721 |
} |
| 722 |
|
| 723 |
free(assets_header); |
| 724 |
|
| 725 |
#if !defined(KODEV_MINIMAL) |
| 726 |
if (bopt->kore_flavor == NULL || |
| 727 |
!strstr(bopt->kore_flavor, "NOTLS=1")) { |
| 728 |
if (!cli_dir_exists("cert")) { |
| 729 |
cli_mkdir("cert", 0700); |
| 730 |
cli_generate_certs(); |
| 731 |
} |
| 732 |
} |
| 733 |
#endif |
| 734 |
|
| 735 |
if (bopt->single_binary) { |
| 736 |
requires_relink++; |
| 737 |
(void)cli_vasprintf(&sofile, "%s/%s", out_dir, appl); |
| 738 |
} else { |
| 739 |
(void)cli_vasprintf(&sofile, "%s/%s.so", out_dir, appl); |
| 740 |
} |
| 741 |
|
| 742 |
if (!cli_file_exists(sofile) && source_files_count > 0) |
| 743 |
requires_relink++; |
| 744 |
|
| 745 |
free(sofile); |
| 746 |
|
| 747 |
if (requires_relink) { |
| 748 |
cli_spawn_proc(cli_link_application, bopt); |
| 749 |
printf("%s built successfully!\n", appl); |
| 750 |
} else { |
| 751 |
printf("nothing to be done!\n"); |
| 752 |
} |
| 753 |
|
| 754 |
if (run_after == 0) |
| 755 |
cli_buildopt_cleanup(); |
| 756 |
} |
| 757 |
|
| 758 |
static void |
| 759 |
cli_source(int argc, char **argv) |
| 760 |
{ |
| 761 |
printf("%s/share/kore/\n", prefix); |
| 762 |
} |
| 763 |
|
| 764 |
static void |
| 765 |
cli_clean(int argc, char **argv) |
| 766 |
{ |
| 767 |
struct buildopt *bopt; |
| 768 |
char pwd[PATH_MAX], *bin; |
| 769 |
|
| 770 |
if (cli_dir_exists(object_dir)) |
| 771 |
cli_cleanup_files(object_dir); |
| 772 |
|
| 773 |
if (getcwd(pwd, sizeof(pwd)) == NULL) |
| 774 |
fatal("could not get cwd: %s", errno_s); |
| 775 |
|
| 776 |
appl = basename(pwd); |
| 777 |
|
| 778 |
TAILQ_INIT(&mime_types); |
| 779 |
TAILQ_INIT(&build_options); |
| 780 |
|
| 781 |
cli_flavor_load(); |
| 782 |
bopt = cli_buildopt_new("_default"); |
| 783 |
cli_buildopt_parse("conf/build.conf"); |
| 784 |
|
| 785 |
if (bopt->single_binary) |
| 786 |
(void)cli_vasprintf(&bin, "%s/%s", out_dir, appl); |
| 787 |
else |
| 788 |
(void)cli_vasprintf(&bin, "%s/%s.so", out_dir, appl); |
| 789 |
|
| 790 |
if (unlink(bin) == -1 && errno != ENOENT) |
| 791 |
printf("couldn't unlink %s: %s", bin, errno_s); |
| 792 |
|
| 793 |
free(bin); |
| 794 |
} |
| 795 |
|
| 796 |
static void |
| 797 |
cli_run(int argc, char **argv) |
| 798 |
{ |
| 799 |
if (cli_file_exists("__init__.py")) { |
| 800 |
cli_run_kore_python(); |
| 801 |
return; |
| 802 |
} |
| 803 |
|
| 804 |
run_after = 1; |
| 805 |
cli_build(argc, argv); |
| 806 |
|
| 807 |
/* |
| 808 |
* We are exec()'ing kore again, while we could technically set |
| 809 |
* the right cli options manually and just continue running. |
| 810 |
*/ |
| 811 |
cli_run_kore(); |
| 812 |
} |
| 813 |
|
| 814 |
static void |
| 815 |
cli_reload(int argc, char **argv) |
| 816 |
{ |
| 817 |
int fd; |
| 818 |
size_t len; |
| 819 |
pid_t pid; |
| 820 |
char *buf; |
| 821 |
|
| 822 |
cli_file_open("kore.pid", O_RDONLY, &fd); |
| 823 |
cli_file_read(fd, &buf, &len); |
| 824 |
cli_file_close(fd); |
| 825 |
|
| 826 |
if (len == 0) |
| 827 |
fatal("reload: pid file is empty"); |
| 828 |
|
| 829 |
buf[len - 1] = '\0'; |
| 830 |
|
| 831 |
pid = cli_strtonum(buf, 0, UINT_MAX); |
| 832 |
|
| 833 |
if (kill(pid, SIGHUP) == -1) |
| 834 |
fatal("failed to reload: %s", errno_s); |
| 835 |
|
| 836 |
printf("reloaded application\n"); |
| 837 |
} |
| 838 |
|
| 839 |
static void |
| 840 |
cli_info(int argc, char **argv) |
| 841 |
{ |
| 842 |
size_t len; |
| 843 |
struct buildopt *bopt; |
| 844 |
char *features; |
| 845 |
|
| 846 |
TAILQ_INIT(&mime_types); |
| 847 |
TAILQ_INIT(&build_options); |
| 848 |
|
| 849 |
cli_flavor_load(); |
| 850 |
bopt = cli_buildopt_new("_default"); |
| 851 |
cli_buildopt_parse("conf/build.conf"); |
| 852 |
|
| 853 |
printf("active flavor\t %s\n", flavor); |
| 854 |
printf("output type \t %s\n", |
| 855 |
(bopt->single_binary) ? "binary" : "dso"); |
| 856 |
|
| 857 |
if (bopt->single_binary) { |
| 858 |
printf("kore features\t %s\n", bopt->kore_flavor); |
| 859 |
printf("kore source \t %s\n", bopt->kore_source); |
| 860 |
} else { |
| 861 |
cli_kore_load_file("features", bopt, &features, &len); |
| 862 |
printf("kore binary \t %s/bin/kore\n", prefix); |
| 863 |
printf("kore features\t %.*s\n", (int)len, features); |
| 864 |
free(features); |
| 865 |
} |
| 866 |
} |
| 867 |
|
| 868 |
static void |
| 869 |
cli_cflags(int argc, char **argv) |
| 870 |
{ |
| 871 |
struct cli_buf *buf; |
| 872 |
|
| 873 |
buf = cli_buf_alloc(128); |
| 874 |
cli_build_flags_common(NULL, buf); |
| 875 |
printf("%.*s\n", (int)buf->offset, buf->data); |
| 876 |
cli_buf_free(buf); |
| 877 |
} |
| 878 |
|
| 879 |
static void |
| 880 |
cli_ldflags(int argc, char **argv) |
| 881 |
{ |
| 882 |
char *p; |
| 883 |
size_t len; |
| 884 |
|
| 885 |
cli_kore_load_file("linker", NULL, &p, &len); |
| 886 |
printf("%.*s ", (int)len, p); |
| 887 |
|
| 888 |
#if defined(__MACH__) |
| 889 |
printf("-dynamiclib -undefined suppress -flat_namespace "); |
| 890 |
#else |
| 891 |
printf("-shared "); |
| 892 |
#endif |
| 893 |
printf("\n"); |
| 894 |
|
| 895 |
free(p); |
| 896 |
} |
| 897 |
|
| 898 |
static void |
| 899 |
cli_genasset(int argc, char **argv) |
| 900 |
{ |
| 901 |
struct stat st; |
| 902 |
struct dirent dp; |
| 903 |
char *hdr; |
| 904 |
|
| 905 |
genasset_cmd = 1; |
| 906 |
TAILQ_INIT(&build_options); |
| 907 |
(void)cli_buildopt_new("_default"); |
| 908 |
|
| 909 |
if (getenv("KORE_OBJDIR") == NULL) |
| 910 |
object_dir = out_dir; |
| 911 |
|
| 912 |
if (argv[0] == NULL) |
| 913 |
cli_genasset_help(); |
| 914 |
|
| 915 |
(void)cli_vasprintf(&hdr, "%s/assets.h", out_dir); |
| 916 |
(void)unlink(hdr); |
| 917 |
|
| 918 |
cli_file_open(hdr, O_CREAT | O_TRUNC | O_WRONLY, &s_fd); |
| 919 |
cli_file_writef(s_fd, "#ifndef __H_KORE_ASSETS_H\n"); |
| 920 |
cli_file_writef(s_fd, "#define __H_KORE_ASSETS_H\n"); |
| 921 |
|
| 922 |
if (stat(argv[0], &st) == -1) |
| 923 |
fatal("%s: %s", argv[0], errno_s); |
| 924 |
|
| 925 |
if (S_ISDIR(st.st_mode)) { |
| 926 |
if (cli_dir_exists(argv[0])) |
| 927 |
cli_find_files(argv[0], cli_build_asset); |
| 928 |
} else if (S_ISREG(st.st_mode)) { |
| 929 |
memset(&dp, 0, sizeof(dp)); |
| 930 |
dp.d_type = DT_REG; |
| 931 |
(void)snprintf(dp.d_name, sizeof(dp.d_name), "%s", |
| 932 |
basename(argv[0])); |
| 933 |
cli_build_asset(argv[0], &dp); |
| 934 |
} else { |
| 935 |
fatal("%s is not a directory or regular file", argv[0]); |
| 936 |
} |
| 937 |
|
| 938 |
cli_file_writef(s_fd, "\n#endif\n"); |
| 939 |
cli_file_close(s_fd); |
| 940 |
} |
| 941 |
|
| 942 |
static void |
| 943 |
cli_genasset_help(void) |
| 944 |
{ |
| 945 |
printf("Usage: kodev genasset [source]\n"); |
| 946 |
printf("Synopsis:\n"); |
| 947 |
printf(" Generates asset file(s) to be used for compilation.\n"); |
| 948 |
printf(" The source can be a single file or directory.\n"); |
| 949 |
printf("\n"); |
| 950 |
printf("This command honors the KODEV_OUTPUT environment variable.\n"); |
| 951 |
printf("This command honors the KORE_OBJDIR environment variable.\n"); |
| 952 |
|
| 953 |
exit(1); |
| 954 |
} |
| 955 |
|
| 956 |
#if !defined(KODEV_MINIMAL) |
| 957 |
static void |
| 958 |
file_create_python_src(void) |
| 959 |
{ |
| 960 |
char *name; |
| 961 |
|
| 962 |
(void)cli_vasprintf(&name, "%s/__init__.py", appl); |
| 963 |
cli_file_create(name, python_init_data, strlen(python_init_data)); |
| 964 |
free(name); |
| 965 |
|
| 966 |
(void)cli_vasprintf(&name, "%s/app.py", appl); |
| 967 |
cli_file_create(name, python_app_data, strlen(python_app_data)); |
| 968 |
free(name); |
| 969 |
} |
| 970 |
|
| 971 |
static void |
| 972 |
file_create_src(void) |
| 973 |
{ |
| 974 |
char *name; |
| 975 |
|
| 976 |
(void)cli_vasprintf(&name, "%s/src/%s.c", appl, appl); |
| 977 |
cli_file_create(name, src_data, strlen(src_data)); |
| 978 |
free(name); |
| 979 |
} |
| 980 |
|
| 981 |
static void |
| 982 |
file_create_config(void) |
| 983 |
{ |
| 984 |
int l; |
| 985 |
char *name, *data; |
| 986 |
|
| 987 |
(void)cli_vasprintf(&name, "%s/conf/%s.conf", appl, appl); |
| 988 |
l = cli_vasprintf(&data, config_data, appl, appl); |
| 989 |
cli_file_create(name, data, l); |
| 990 |
free(name); |
| 991 |
free(data); |
| 992 |
|
| 993 |
(void)cli_vasprintf(&name, "%s/conf/build.conf", appl); |
| 994 |
l = cli_vasprintf(&data, build_data, appl); |
| 995 |
cli_file_create(name, data, l); |
| 996 |
free(name); |
| 997 |
free(data); |
| 998 |
} |
| 999 |
|
| 1000 |
static void |
| 1001 |
file_create_gitignore(void) |
| 1002 |
{ |
| 1003 |
int l; |
| 1004 |
char *name, *data; |
| 1005 |
|
| 1006 |
(void)cli_vasprintf(&name, "%s/.gitignore", appl); |
| 1007 |
l = cli_vasprintf(&data, gitignore, appl); |
| 1008 |
cli_file_create(name, data, l); |
| 1009 |
free(name); |
| 1010 |
free(data); |
| 1011 |
} |
| 1012 |
#endif |
| 1013 |
|
| 1014 |
static void |
| 1015 |
cli_mkdir(const char *fpath, int mode) |
| 1016 |
{ |
| 1017 |
if (mkdir(fpath, mode) == -1) |
| 1018 |
fatal("cli_mkdir(%s): %s", fpath, errno_s); |
| 1019 |
} |
| 1020 |
|
| 1021 |
static int |
| 1022 |
cli_file_exists(const char *fpath) |
| 1023 |
{ |
| 1024 |
struct stat st; |
| 1025 |
|
| 1026 |
if (stat(fpath, &st) == -1) |
| 1027 |
return (0); |
| 1028 |
|
| 1029 |
if (!S_ISREG(st.st_mode)) |
| 1030 |
return (0); |
| 1031 |
|
| 1032 |
return (1); |
| 1033 |
} |
| 1034 |
|
| 1035 |
static int |
| 1036 |
cli_file_requires_build(struct stat *fst, const char *opath) |
| 1037 |
{ |
| 1038 |
struct stat ost; |
| 1039 |
|
| 1040 |
if (stat(opath, &ost) == -1) { |
| 1041 |
if (errno == ENOENT) |
| 1042 |
return (1); |
| 1043 |
fatal("stat(%s): %s", opath, errno_s); |
| 1044 |
} |
| 1045 |
|
| 1046 |
return (fst->st_mtime != ost.st_mtime); |
| 1047 |
} |
| 1048 |
|
| 1049 |
static int |
| 1050 |
cli_dir_exists(const char *fpath) |
| 1051 |
{ |
| 1052 |
struct stat st; |
| 1053 |
|
| 1054 |
if (stat(fpath, &st) == -1) |
| 1055 |
return (0); |
| 1056 |
|
| 1057 |
if (!S_ISDIR(st.st_mode)) |
| 1058 |
return (0); |
| 1059 |
|
| 1060 |
return (1); |
| 1061 |
} |
| 1062 |
|
| 1063 |
static void |
| 1064 |
cli_file_open(const char *fpath, int flags, int *fd) |
| 1065 |
{ |
| 1066 |
if ((*fd = open(fpath, flags, 0644)) == -1) |
| 1067 |
fatal("cli_file_open(%s): %s", fpath, errno_s); |
| 1068 |
} |
| 1069 |
|
| 1070 |
static void |
| 1071 |
cli_file_read(int fd, char **buf, size_t *len) |
| 1072 |
{ |
| 1073 |
struct stat st; |
| 1074 |
char *p; |
| 1075 |
ssize_t ret; |
| 1076 |
size_t offset, bytes; |
| 1077 |
|
| 1078 |
if (fstat(fd, &st) == -1) |
| 1079 |
fatal("fstat(): %s", errno_s); |
| 1080 |
|
| 1081 |
if (st.st_size > USHRT_MAX) |
| 1082 |
fatal("cli_file_read: way too big"); |
| 1083 |
|
| 1084 |
offset = 0; |
| 1085 |
bytes = st.st_size; |
| 1086 |
p = cli_malloc(bytes); |
| 1087 |
|
| 1088 |
while (offset != bytes) { |
| 1089 |
ret = read(fd, p + offset, bytes - offset); |
| 1090 |
if (ret == -1) { |
| 1091 |
if (errno == EINTR) |
| 1092 |
continue; |
| 1093 |
fatal("read(): %s", errno_s); |
| 1094 |
} |
| 1095 |
|
| 1096 |
if (ret == 0) |
| 1097 |
fatal("unexpected EOF"); |
| 1098 |
|
| 1099 |
offset += (size_t)ret; |
| 1100 |
} |
| 1101 |
|
| 1102 |
*buf = p; |
| 1103 |
*len = bytes; |
| 1104 |
} |
| 1105 |
|
| 1106 |
static void |
| 1107 |
cli_file_close(int fd) |
| 1108 |
{ |
| 1109 |
if (close(fd) == -1) |
| 1110 |
printf("warning: close() %s\n", errno_s); |
| 1111 |
} |
| 1112 |
|
| 1113 |
static void |
| 1114 |
cli_file_writef(int fd, const char *fmt, ...) |
| 1115 |
{ |
| 1116 |
int l; |
| 1117 |
char *buf; |
| 1118 |
va_list args; |
| 1119 |
|
| 1120 |
va_start(args, fmt); |
| 1121 |
l = vasprintf(&buf, fmt, args); |
| 1122 |
va_end(args); |
| 1123 |
|
| 1124 |
if (l == -1) |
| 1125 |
fatal("cli_file_writef"); |
| 1126 |
|
| 1127 |
cli_file_write(fd, buf, l); |
| 1128 |
free(buf); |
| 1129 |
} |
| 1130 |
|
| 1131 |
static void |
| 1132 |
cli_file_write(int fd, const void *buf, size_t len) |
| 1133 |
{ |
| 1134 |
ssize_t r; |
| 1135 |
const u_int8_t *d; |
| 1136 |
size_t written; |
| 1137 |
|
| 1138 |
d = buf; |
| 1139 |
written = 0; |
| 1140 |
while (written != len) { |
| 1141 |
r = write(fd, d + written, len - written); |
| 1142 |
if (r == -1) { |
| 1143 |
if (errno == EINTR) |
| 1144 |
continue; |
| 1145 |
fatal("cli_file_write: %s", errno_s); |
| 1146 |
} |
| 1147 |
|
| 1148 |
written += r; |
| 1149 |
} |
| 1150 |
} |
| 1151 |
|
| 1152 |
#if !defined(KODEV_MINIMAL) |
| 1153 |
static void |
| 1154 |
cli_file_create(const char *name, const char *data, size_t len) |
| 1155 |
{ |
| 1156 |
int fd; |
| 1157 |
|
| 1158 |
cli_file_open(name, O_CREAT | O_TRUNC | O_WRONLY, &fd); |
| 1159 |
cli_file_write(fd, data, len); |
| 1160 |
cli_file_close(fd); |
| 1161 |
|
| 1162 |
printf("created %s\n", name); |
| 1163 |
} |
| 1164 |
#endif |
| 1165 |
|
| 1166 |
static void |
| 1167 |
cli_write_asset(const char *n, const char *e, struct buildopt *bopt) |
| 1168 |
{ |
| 1169 |
cli_file_writef(s_fd, "extern const u_int8_t asset_%s_%s[];\n", n, e); |
| 1170 |
cli_file_writef(s_fd, "extern const u_int32_t asset_len_%s_%s;\n", n, e); |
| 1171 |
cli_file_writef(s_fd, "extern const time_t asset_mtime_%s_%s;\n", n, e); |
| 1172 |
|
| 1173 |
#if !defined(KODEV_MINIMAL) |
| 1174 |
cli_file_writef(s_fd, "extern const char *asset_sha256_%s_%s;\n", n, e); |
| 1175 |
#endif |
| 1176 |
|
| 1177 |
if (bopt->flavor_nohttp == 0) { |
| 1178 |
cli_file_writef(s_fd, |
| 1179 |
"int asset_serve_%s_%s(struct http_request *);\n", n, e); |
| 1180 |
} |
| 1181 |
} |
| 1182 |
|
| 1183 |
static void |
| 1184 |
cli_build_asset(char *fpath, struct dirent *dp) |
| 1185 |
{ |
| 1186 |
u_int8_t *d; |
| 1187 |
struct stat st; |
| 1188 |
#if !defined(KODEV_MINIMAL) |
| 1189 |
SHA256_CTX sctx; |
| 1190 |
int i, len; |
| 1191 |
struct mime_type *mime; |
| 1192 |
const char *mime_type; |
| 1193 |
u_int8_t digest[SHA256_DIGEST_LENGTH]; |
| 1194 |
char hash[(SHA256_DIGEST_LENGTH * 2) + 1]; |
| 1195 |
#endif |
| 1196 |
off_t off; |
| 1197 |
void *base; |
| 1198 |
struct buildopt *bopt; |
| 1199 |
int in, out; |
| 1200 |
char *cpath, *ext, *opath, *p, *name; |
| 1201 |
|
| 1202 |
bopt = cli_buildopt_default(); |
| 1203 |
|
| 1204 |
/* Ignore hidden files and some editor files */ |
| 1205 |
if (dp->d_name[0] == '.' || |
| 1206 |
strrchr(dp->d_name, '~') || strrchr(dp->d_name, '#')) { |
| 1207 |
return; |
| 1208 |
} |
| 1209 |
|
| 1210 |
name = cli_strdup(dp->d_name); |
| 1211 |
|
| 1212 |
/* Grab the extension as we're using it in the symbol name. */ |
| 1213 |
if ((ext = strrchr(name, '.')) == NULL) |
| 1214 |
fatal("couldn't find ext in %s", name); |
| 1215 |
|
| 1216 |
/* Replace dots, spaces, etc etc with underscores. */ |
| 1217 |
for (p = name; *p != '\0'; p++) { |
| 1218 |
if (*p == '.' || isspace((unsigned char)*p) || *p == '-') |
| 1219 |
*p = '_'; |
| 1220 |
} |
| 1221 |
|
| 1222 |
/* Grab inode information. */ |
| 1223 |
if (stat(fpath, &st) == -1) |
| 1224 |
fatal("stat: %s %s", fpath, errno_s); |
| 1225 |
|
| 1226 |
/* If this file was empty, skip it. */ |
| 1227 |
if (st.st_size == 0) { |
| 1228 |
printf("skipping empty asset %s\n", name); |
| 1229 |
free(name); |
| 1230 |
return; |
| 1231 |
} |
| 1232 |
|
| 1233 |
(void)cli_vasprintf(&opath, "%s/%s.o", object_dir, name); |
| 1234 |
(void)cli_vasprintf(&cpath, "%s/%s.c", object_dir, name); |
| 1235 |
|
| 1236 |
/* Check if the file needs to be built. */ |
| 1237 |
if (!cli_file_requires_build(&st, opath)) { |
| 1238 |
*(ext)++ = '\0'; |
| 1239 |
cli_write_asset(name, ext, bopt); |
| 1240 |
*ext = '_'; |
| 1241 |
|
| 1242 |
cli_add_source_file(name, cpath, opath, &st, BUILD_NOBUILD); |
| 1243 |
free(name); |
| 1244 |
return; |
| 1245 |
} |
| 1246 |
|
| 1247 |
/* Open the file we're converting. */ |
| 1248 |
cli_file_open(fpath, O_RDONLY, &in); |
| 1249 |
|
| 1250 |
/* mmap our in file. */ |
| 1251 |
if ((base = mmap(NULL, st.st_size, |
| 1252 |
PROT_READ, MAP_PRIVATE, in, 0)) == MAP_FAILED) |
| 1253 |
fatal("mmap: %s %s", fpath, errno_s); |
| 1254 |
|
| 1255 |
/* Create the c file where we will write too. */ |
| 1256 |
cli_file_open(cpath, O_CREAT | O_TRUNC | O_WRONLY, &out); |
| 1257 |
|
| 1258 |
/* No longer need name so cut off the extension. */ |
| 1259 |
printf("building asset %s\n", dp->d_name); |
| 1260 |
*(ext)++ = '\0'; |
| 1261 |
|
| 1262 |
/* Start generating the file. */ |
| 1263 |
cli_file_writef(out, "/* Auto generated */\n"); |
| 1264 |
cli_file_writef(out, "#include <sys/types.h>\n\n"); |
| 1265 |
cli_file_writef(out, "#include <kore/kore.h>\n"); |
| 1266 |
cli_file_writef(out, "#include <kore/http.h>\n\n"); |
| 1267 |
cli_file_writef(out, "#include \"assets.h\"\n\n"); |
| 1268 |
|
| 1269 |
/* Write the file data as a byte array. */ |
| 1270 |
cli_file_writef(out, "const u_int8_t asset_%s_%s[] = {\n", name, ext); |
| 1271 |
d = base; |
| 1272 |
for (off = 0; off < st.st_size; off++) |
| 1273 |
cli_file_writef(out, "0x%02x,", *d++); |
| 1274 |
|
| 1275 |
/* |
| 1276 |
* Always NUL-terminate the asset, even if this NUL is not included in |
| 1277 |
* the actual length. This way assets can be cast to char * without |
| 1278 |
* any additional thinking for the developer. |
| 1279 |
*/ |
| 1280 |
cli_file_writef(out, "0x00"); |
| 1281 |
|
| 1282 |
#if !defined(KODEV_MINIMAL) |
| 1283 |
/* Calculate the SHA256 digest of the contents. */ |
| 1284 |
(void)SHA256_Init(&sctx); |
| 1285 |
(void)SHA256_Update(&sctx, base, st.st_size); |
| 1286 |
(void)SHA256_Final(digest, &sctx); |
| 1287 |
|
| 1288 |
for (i = 0; i < (int)sizeof(digest); i++) { |
| 1289 |
len = snprintf(hash + (i * 2), sizeof(hash) - (i * 2), |
| 1290 |
"%02x", digest[i]); |
| 1291 |
if (len == -1 || (size_t)len >= sizeof(hash)) |
| 1292 |
fatal("failed to convert SHA256 digest to hex"); |
| 1293 |
} |
| 1294 |
|
| 1295 |
mime = NULL; |
| 1296 |
TAILQ_FOREACH(mime, &mime_types, list) { |
| 1297 |
if (!strcasecmp(mime->ext, ext)) |
| 1298 |
break; |
| 1299 |
} |
| 1300 |
|
| 1301 |
if (mime != NULL) |
| 1302 |
mime_type = mime->type; |
| 1303 |
else |
| 1304 |
mime_type = "text/plain"; |
| 1305 |
#endif |
| 1306 |
|
| 1307 |
/* Add the meta data. */ |
| 1308 |
cli_file_writef(out, "};\n\n"); |
| 1309 |
cli_file_writef(out, "const u_int32_t asset_len_%s_%s = %" PRIu32 ";\n", |
| 1310 |
name, ext, (u_int32_t)st.st_size); |
| 1311 |
cli_file_writef(out, |
| 1312 |
"const time_t asset_mtime_%s_%s = %" PRId64 ";\n", |
| 1313 |
name, ext, (int64_t)st.st_mtime); |
| 1314 |
|
| 1315 |
#if !defined(KODEV_MINIMAL) |
| 1316 |
if (bopt->flavor_nohttp == 0) { |
| 1317 |
cli_file_writef(out, |
| 1318 |
"const char *asset_sha256_%s_%s = \"\\\"%s\\\"\";\n", |
| 1319 |
name, ext, hash); |
| 1320 |
cli_file_writef(out, http_serveable_function, |
| 1321 |
name, ext, name, ext, name, ext, name, ext, mime_type); |
| 1322 |
} |
| 1323 |
#endif |
| 1324 |
|
| 1325 |
/* Write the file symbols into assets.h so they can be used. */ |
| 1326 |
cli_write_asset(name, ext, bopt); |
| 1327 |
|
| 1328 |
/* Cleanup static file source. */ |
| 1329 |
if (munmap(base, st.st_size) == -1) |
| 1330 |
fatal("munmap: %s %s", fpath, errno_s); |
| 1331 |
|
| 1332 |
/* Cleanup fds */ |
| 1333 |
cli_file_close(in); |
| 1334 |
cli_file_close(out); |
| 1335 |
|
| 1336 |
/* Restore the original name */ |
| 1337 |
*--ext = '.'; |
| 1338 |
|
| 1339 |
/* Register the .c file now (cpath is free'd later). */ |
| 1340 |
if (genasset_cmd == 0) |
| 1341 |
cli_add_source_file(name, cpath, opath, &st, BUILD_C); |
| 1342 |
|
| 1343 |
free(name); |
| 1344 |
} |
| 1345 |
|
| 1346 |
static void |
| 1347 |
cli_add_source_file(char *name, char *fpath, char *opath, struct stat *st, |
| 1348 |
int build) |
| 1349 |
{ |
| 1350 |
struct cfile *cf; |
| 1351 |
|
| 1352 |
source_files_count++; |
| 1353 |
cf = cli_malloc(sizeof(*cf)); |
| 1354 |
|
| 1355 |
if (st != NULL) |
| 1356 |
cf->st = *st; |
| 1357 |
else |
| 1358 |
memset(&cf->st, 0, sizeof(cf->st)); |
| 1359 |
|
| 1360 |
cf->build = build; |
| 1361 |
cf->fpath = fpath; |
| 1362 |
cf->opath = opath; |
| 1363 |
cf->name = cli_strdup(name); |
| 1364 |
|
| 1365 |
TAILQ_INSERT_TAIL(&source_files, cf, list); |
| 1366 |
} |
| 1367 |
|
| 1368 |
static void |
| 1369 |
cli_register_source_file(char *fpath, struct dirent *dp) |
| 1370 |
{ |
| 1371 |
struct stat st; |
| 1372 |
char *ext, *opath; |
| 1373 |
int build; |
| 1374 |
|
| 1375 |
if ((ext = strrchr(fpath, '.')) == NULL || |
| 1376 |
(strcmp(ext, ".c") && strcmp(ext, ".cpp"))) |
| 1377 |
return; |
| 1378 |
|
| 1379 |
if (stat(fpath, &st) == -1) |
| 1380 |
fatal("stat(%s): %s", fpath, errno_s); |
| 1381 |
|
| 1382 |
if (!strcmp(ext, ".cpp")) |
| 1383 |
cxx_files_count++; |
| 1384 |
|
| 1385 |
(void)cli_vasprintf(&opath, "%s/%s.o", object_dir, dp->d_name); |
| 1386 |
if (!cli_file_requires_build(&st, opath)) { |
| 1387 |
build = BUILD_NOBUILD; |
| 1388 |
} else if (!strcmp(ext, ".cpp")) { |
| 1389 |
build = BUILD_CXX; |
| 1390 |
} else { |
| 1391 |
build = BUILD_C; |
| 1392 |
} |
| 1393 |
|
| 1394 |
cli_add_source_file(dp->d_name, fpath, opath, &st, build); |
| 1395 |
} |
| 1396 |
|
| 1397 |
static void |
| 1398 |
cli_register_kore_file(char *fpath, struct dirent *dp) |
| 1399 |
{ |
| 1400 |
struct stat st, ost; |
| 1401 |
char *opath, *ext, *fname; |
| 1402 |
|
| 1403 |
if ((ext = strrchr(fpath, '.')) == NULL || strcmp(ext, ".c")) |
| 1404 |
return; |
| 1405 |
|
| 1406 |
if (stat(fpath, &st) == -1) |
| 1407 |
fatal("stat(%s): %s", fpath, errno_s); |
| 1408 |
|
| 1409 |
*ext = '\0'; |
| 1410 |
if ((fname = basename(fpath)) == NULL) |
| 1411 |
fatal("basename failed"); |
| 1412 |
|
| 1413 |
(void)cli_vasprintf(&opath, "%s/%s.o", object_dir, fname); |
| 1414 |
|
| 1415 |
/* Silently ignore non existing object files for kore source files. */ |
| 1416 |
if (stat(opath, &ost) == -1) { |
| 1417 |
free(opath); |
| 1418 |
return; |
| 1419 |
} |
| 1420 |
|
| 1421 |
cli_add_source_file(dp->d_name, fpath, opath, &st, BUILD_NOBUILD); |
| 1422 |
} |
| 1423 |
|
| 1424 |
static void |
| 1425 |
cli_file_remove(char *fpath, struct dirent *dp) |
| 1426 |
{ |
| 1427 |
if (unlink(fpath) == -1) |
| 1428 |
fprintf(stderr, "couldn't unlink %s: %s", fpath, errno_s); |
| 1429 |
} |
| 1430 |
|
| 1431 |
static void |
| 1432 |
cli_find_files(const char *path, void (*cb)(char *, struct dirent *)) |
| 1433 |
{ |
| 1434 |
DIR *d; |
| 1435 |
struct stat st; |
| 1436 |
struct dirent *dp; |
| 1437 |
char *fpath; |
| 1438 |
|
| 1439 |
if ((d = opendir(path)) == NULL) |
| 1440 |
fatal("cli_find_files: opendir(%s): %s", path, errno_s); |
| 1441 |
|
| 1442 |
while ((dp = readdir(d)) != NULL) { |
| 1443 |
if (!strcmp(dp->d_name, ".") || |
| 1444 |
!strcmp(dp->d_name, "..")) |
| 1445 |
continue; |
| 1446 |
|
| 1447 |
(void)cli_vasprintf(&fpath, "%s/%s", path, dp->d_name); |
| 1448 |
if (stat(fpath, &st) == -1) { |
| 1449 |
fprintf(stderr, "stat(%s): %s\n", fpath, errno_s); |
| 1450 |
free(fpath); |
| 1451 |
continue; |
| 1452 |
} |
| 1453 |
|
| 1454 |
if (S_ISDIR(st.st_mode)) { |
| 1455 |
cli_find_files(fpath, cb); |
| 1456 |
free(fpath); |
| 1457 |
} else if (S_ISREG(st.st_mode)) { |
| 1458 |
cb(fpath, dp); |
| 1459 |
} else { |
| 1460 |
fprintf(stderr, "ignoring %s\n", fpath); |
| 1461 |
free(fpath); |
| 1462 |
} |
| 1463 |
} |
| 1464 |
|
| 1465 |
closedir(d); |
| 1466 |
} |
| 1467 |
|
| 1468 |
#if !defined(KODEV_MINIMAL) |
| 1469 |
static void |
| 1470 |
cli_generate_certs(void) |
| 1471 |
{ |
| 1472 |
BIGNUM *e; |
| 1473 |
FILE *fp; |
| 1474 |
time_t now; |
| 1475 |
X509_NAME *name; |
| 1476 |
EVP_PKEY *pkey; |
| 1477 |
X509 *x509; |
| 1478 |
RSA *kpair; |
| 1479 |
char issuer[64]; |
| 1480 |
|
| 1481 |
/* Create new certificate. */ |
| 1482 |
if ((x509 = X509_new()) == NULL) |
| 1483 |
fatal("X509_new(): %s", ssl_errno_s); |
| 1484 |
|
| 1485 |
/* Generate version 3. */ |
| 1486 |
if (!X509_set_version(x509, 2)) |
| 1487 |
fatal("X509_set_version(): %s", ssl_errno_s); |
| 1488 |
|
| 1489 |
/* Generate RSA keys. */ |
| 1490 |
if ((pkey = EVP_PKEY_new()) == NULL) |
| 1491 |
fatal("EVP_PKEY_new(): %s", ssl_errno_s); |
| 1492 |
if ((kpair = RSA_new()) == NULL) |
| 1493 |
fatal("RSA_new(): %s", ssl_errno_s); |
| 1494 |
if ((e = BN_new()) == NULL) |
| 1495 |
fatal("BN_new(): %s", ssl_errno_s); |
| 1496 |
|
| 1497 |
if (!BN_set_word(e, 65537)) |
| 1498 |
fatal("BN_set_word(): %s", ssl_errno_s); |
| 1499 |
if (!RSA_generate_key_ex(kpair, 2048, e, NULL)) |
| 1500 |
fatal("RSA_generate_key_ex(): %s", ssl_errno_s); |
| 1501 |
|
| 1502 |
BN_free(e); |
| 1503 |
|
| 1504 |
if (!EVP_PKEY_assign_RSA(pkey, kpair)) |
| 1505 |
fatal("EVP_PKEY_assign_RSA(): %s", ssl_errno_s); |
| 1506 |
|
| 1507 |
/* Set serial number to current timestamp. */ |
| 1508 |
time(&now); |
| 1509 |
if (!ASN1_INTEGER_set(X509_get_serialNumber(x509), now)) |
| 1510 |
fatal("ASN1_INTEGER_set(): %s", ssl_errno_s); |
| 1511 |
|
| 1512 |
/* Not before and not after dates. */ |
| 1513 |
if (!X509_gmtime_adj(X509_get_notBefore(x509), 0)) |
| 1514 |
fatal("X509_gmtime_adj(): %s", ssl_errno_s); |
| 1515 |
if (!X509_gmtime_adj(X509_get_notAfter(x509), |
| 1516 |
(long)60 * 60 * 24 * 3000)) |
| 1517 |
fatal("X509_gmtime_adj(): %s", ssl_errno_s); |
| 1518 |
|
| 1519 |
/* Attach the pkey to the certificate. */ |
| 1520 |
if (!X509_set_pubkey(x509, pkey)) |
| 1521 |
fatal("X509_set_pubkey(): %s", ssl_errno_s); |
| 1522 |
|
| 1523 |
/* Set certificate information. */ |
| 1524 |
if ((name = X509_get_subject_name(x509)) == NULL) |
| 1525 |
fatal("X509_get_subject_name(): %s", ssl_errno_s); |
| 1526 |
|
| 1527 |
(void)snprintf(issuer, sizeof(issuer), "kore autogen: %s", appl); |
| 1528 |
if (!X509_NAME_add_entry_by_txt(name, "C", |
| 1529 |
MBSTRING_ASC, (const unsigned char *)"SE", -1, -1, 0)) |
| 1530 |
fatal("X509_NAME_add_entry_by_txt(): C %s", ssl_errno_s); |
| 1531 |
if (!X509_NAME_add_entry_by_txt(name, "O", |
| 1532 |
MBSTRING_ASC, (const unsigned char *)issuer, -1, -1, 0)) |
| 1533 |
fatal("X509_NAME_add_entry_by_txt(): O %s", ssl_errno_s); |
| 1534 |
if (!X509_NAME_add_entry_by_txt(name, "CN", |
| 1535 |
MBSTRING_ASC, (const unsigned char *)"localhost", -1, -1, 0)) |
| 1536 |
fatal("X509_NAME_add_entry_by_txt(): CN %s", ssl_errno_s); |
| 1537 |
|
| 1538 |
if (!X509_set_issuer_name(x509, name)) |
| 1539 |
fatal("X509_set_issuer_name(): %s", ssl_errno_s); |
| 1540 |
|
| 1541 |
if (!X509_sign(x509, pkey, EVP_sha256())) |
| 1542 |
fatal("X509_sign(): %s", ssl_errno_s); |
| 1543 |
|
| 1544 |
if ((fp = fopen("cert/key.pem", "w")) == NULL) |
| 1545 |
fatal("fopen(cert/key.pem): %s", errno_s); |
| 1546 |
if (!PEM_write_PrivateKey(fp, pkey, NULL, NULL, 0, NULL, NULL)) |
| 1547 |
fatal("PEM_write_PrivateKey(): %s", ssl_errno_s); |
| 1548 |
fclose(fp); |
| 1549 |
|
| 1550 |
if ((fp = fopen("cert/server.pem", "w")) == NULL) |
| 1551 |
fatal("fopen(cert/server.pem): %s", errno_s); |
| 1552 |
if (!PEM_write_X509(fp, x509)) |
| 1553 |
fatal("PEM_write_X509(%s)", errno_s); |
| 1554 |
fclose(fp); |
| 1555 |
|
| 1556 |
EVP_PKEY_free(pkey); |
| 1557 |
X509_free(x509); |
| 1558 |
} |
| 1559 |
#endif |
| 1560 |
|
| 1561 |
static void |
| 1562 |
cli_compile_source_file(void *arg) |
| 1563 |
{ |
| 1564 |
struct cfile *cf; |
| 1565 |
int idx, i; |
| 1566 |
char **flags; |
| 1567 |
char *compiler; |
| 1568 |
int flags_count; |
| 1569 |
char *args[34 + CFLAGS_MAX]; |
| 1570 |
|
| 1571 |
cf = arg; |
| 1572 |
|
| 1573 |
switch (cf->build) { |
| 1574 |
case BUILD_C: |
| 1575 |
compiler = compiler_c; |
| 1576 |
flags = cflags; |
| 1577 |
flags_count = cflags_count; |
| 1578 |
break; |
| 1579 |
case BUILD_CXX: |
| 1580 |
compiler = compiler_cpp; |
| 1581 |
flags = cxxflags; |
| 1582 |
flags_count = cxxflags_count; |
| 1583 |
break; |
| 1584 |
default: |
| 1585 |
fatal("cli_compile_file: unexpected file type: %d", |
| 1586 |
cf->build); |
| 1587 |
break; |
| 1588 |
} |
| 1589 |
|
| 1590 |
idx = 0; |
| 1591 |
args[idx++] = compiler; |
| 1592 |
|
| 1593 |
for (i = 0; i < flags_count; i++) |
| 1594 |
args[idx++] = flags[i]; |
| 1595 |
|
| 1596 |
args[idx++] = "-I"; |
| 1597 |
args[idx++] = object_dir; |
| 1598 |
args[idx++] = "-c"; |
| 1599 |
args[idx++] = cf->fpath; |
| 1600 |
args[idx++] = "-o"; |
| 1601 |
args[idx++] = cf->opath; |
| 1602 |
args[idx] = NULL; |
| 1603 |
|
| 1604 |
execvp(compiler, args); |
| 1605 |
fatal("failed to start '%s': %s", compiler, errno_s); |
| 1606 |
} |
| 1607 |
|
| 1608 |
static void |
| 1609 |
cli_link_application(void *arg) |
| 1610 |
{ |
| 1611 |
struct cfile *cf; |
| 1612 |
struct buildopt *bopt; |
| 1613 |
int idx, i; |
| 1614 |
char *output; |
| 1615 |
char *args[source_files_count + 11 + LD_FLAGS_MAX]; |
| 1616 |
|
| 1617 |
bopt = arg; |
| 1618 |
|
| 1619 |
if (bopt->single_binary) |
| 1620 |
(void)cli_vasprintf(&output, "%s/%s", out_dir, appl); |
| 1621 |
else |
| 1622 |
(void)cli_vasprintf(&output, "%s/%s.so", out_dir, appl); |
| 1623 |
|
| 1624 |
idx = 0; |
| 1625 |
args[idx++] = compiler_ld; |
| 1626 |
|
| 1627 |
TAILQ_FOREACH(cf, &source_files, list) |
| 1628 |
args[idx++] = cf->opath; |
| 1629 |
|
| 1630 |
for (i = 0; i < ldflags_count; i++) |
| 1631 |
args[idx++] = ldflags[i]; |
| 1632 |
|
| 1633 |
args[idx++] = "-o"; |
| 1634 |
args[idx++] = output; |
| 1635 |
args[idx] = NULL; |
| 1636 |
|
| 1637 |
execvp(compiler_ld, args); |
| 1638 |
fatal("failed to start '%s': %s", compiler_ld, errno_s); |
| 1639 |
} |
| 1640 |
|
| 1641 |
static void |
| 1642 |
cli_compile_kore(void *arg) |
| 1643 |
{ |
| 1644 |
struct buildopt *bopt = arg; |
| 1645 |
int idx, i, fcnt; |
| 1646 |
char pwd[MAXPATHLEN], *obj, *args[20], *flavors[7]; |
| 1647 |
|
| 1648 |
if (object_dir[0] != '/') { |
| 1649 |
if (getcwd(pwd, sizeof(pwd)) == NULL) |
| 1650 |
fatal("could not get cwd: %s", errno_s); |
| 1651 |
(void)cli_vasprintf(&obj, "OBJDIR=%s/%s", pwd, object_dir); |
| 1652 |
} else { |
| 1653 |
(void)cli_vasprintf(&obj, "OBJDIR=%s", object_dir); |
| 1654 |
} |
| 1655 |
|
| 1656 |
if (putenv(obj) != 0) |
| 1657 |
fatal("cannot set OBJDIR for building kore"); |
| 1658 |
|
| 1659 |
fcnt = cli_split_string(bopt->kore_flavor, " ", flavors, 7); |
| 1660 |
|
| 1661 |
#if defined(OpenBSD) || defined(__FreeBSD_version) || \ |
| 1662 |
defined(NetBSD) || defined(__DragonFly_version) |
| 1663 |
args[0] = "gmake"; |
| 1664 |
#else |
| 1665 |
args[0] = "make"; |
| 1666 |
#endif |
| 1667 |
|
| 1668 |
args[1] = "-s"; |
| 1669 |
args[2] = "-C"; |
| 1670 |
args[3] = bopt->kore_source; |
| 1671 |
args[4] = "objects"; |
| 1672 |
|
| 1673 |
idx = 5; |
| 1674 |
for (i = 0; i < fcnt; i++) { |
| 1675 |
printf("using flavor %s\n", flavors[i]); |
| 1676 |
args[idx++] = flavors[i]; |
| 1677 |
} |
| 1678 |
|
| 1679 |
args[idx++] = "KORE_SINGLE_BINARY=1"; |
| 1680 |
args[idx] = NULL; |
| 1681 |
|
| 1682 |
execvp(args[0], args); |
| 1683 |
fatal("failed to start '%s': %s", args[0], errno_s); |
| 1684 |
} |
| 1685 |
|
| 1686 |
static void |
| 1687 |
cli_run_kore_python(void) |
| 1688 |
{ |
| 1689 |
char *args[5], *cmd; |
| 1690 |
char pwd[MAXPATHLEN]; |
| 1691 |
|
| 1692 |
(void)cli_vasprintf(&cmd, "%s/bin/kore", prefix); |
| 1693 |
|
| 1694 |
if (getcwd(pwd, sizeof(pwd)) == NULL) |
| 1695 |
fatal("could not get cwd: %s", errno_s); |
| 1696 |
|
| 1697 |
args[0] = cmd; |
| 1698 |
args[1] = pwd; |
| 1699 |
args[2] = NULL; |
| 1700 |
|
| 1701 |
execvp(args[0], args); |
| 1702 |
fatal("failed to start '%s': %s", args[0], errno_s); |
| 1703 |
|
| 1704 |
} |
| 1705 |
|
| 1706 |
static void |
| 1707 |
cli_run_kore(void) |
| 1708 |
{ |
| 1709 |
struct buildopt *bopt; |
| 1710 |
char *args[4], *cpath, *cmd, *flags; |
| 1711 |
|
| 1712 |
bopt = cli_buildopt_default(); |
| 1713 |
|
| 1714 |
if (bopt->single_binary) { |
| 1715 |
cpath = NULL; |
| 1716 |
flags = "-fnr"; |
| 1717 |
(void)cli_vasprintf(&cmd, "./%s", appl); |
| 1718 |
} else { |
| 1719 |
flags = "-fnrc"; |
| 1720 |
(void)cli_vasprintf(&cmd, "%s/bin/kore", prefix); |
| 1721 |
(void)cli_vasprintf(&cpath, "conf/%s.conf", appl); |
| 1722 |
} |
| 1723 |
|
| 1724 |
args[0] = cmd; |
| 1725 |
args[1] = flags; |
| 1726 |
|
| 1727 |
if (cpath != NULL) { |
| 1728 |
args[2] = cpath; |
| 1729 |
args[3] = NULL; |
| 1730 |
} else { |
| 1731 |
args[2] = NULL; |
| 1732 |
} |
| 1733 |
|
| 1734 |
execvp(args[0], args); |
| 1735 |
fatal("failed to start '%s': %s", args[0], errno_s); |
| 1736 |
} |
| 1737 |
|
| 1738 |
static void |
| 1739 |
cli_buildopt_parse(const char *path) |
| 1740 |
{ |
| 1741 |
FILE *fp; |
| 1742 |
const char *env; |
| 1743 |
struct buildopt *bopt; |
| 1744 |
char buf[BUFSIZ], *p, *t; |
| 1745 |
|
| 1746 |
if ((fp = fopen(path, "r")) == NULL) |
| 1747 |
fatal("cli_buildopt_parse: fopen(%s): %s", path, errno_s); |
| 1748 |
|
| 1749 |
bopt = NULL; |
| 1750 |
|
| 1751 |
while ((p = cli_read_line(fp, buf, sizeof(buf))) != NULL) { |
| 1752 |
if (strlen(p) == 0) |
| 1753 |
continue; |
| 1754 |
|
| 1755 |
if (bopt != NULL && !strcmp(p, "}")) { |
| 1756 |
bopt = NULL; |
| 1757 |
continue; |
| 1758 |
} |
| 1759 |
|
| 1760 |
if (bopt == NULL) { |
| 1761 |
if ((t = strchr(p, '=')) != NULL) |
| 1762 |
goto parse_option; |
| 1763 |
if ((t = strchr(p, ' ')) == NULL) |
| 1764 |
fatal("unexpected '%s'", p); |
| 1765 |
*(t)++ = '\0'; |
| 1766 |
if (strcmp(t, "{")) |
| 1767 |
fatal("expected '{', got '%s'", t); |
| 1768 |
bopt = cli_buildopt_new(p); |
| 1769 |
continue; |
| 1770 |
} |
| 1771 |
|
| 1772 |
if ((t = strchr(p, '=')) == NULL) { |
| 1773 |
printf("bad buildopt line: '%s'\n", p); |
| 1774 |
continue; |
| 1775 |
} |
| 1776 |
|
| 1777 |
parse_option: |
| 1778 |
*(t)++ = '\0'; |
| 1779 |
|
| 1780 |
p = cli_text_trim(p, strlen(p)); |
| 1781 |
t = cli_text_trim(t, strlen(t)); |
| 1782 |
|
| 1783 |
if (!strcasecmp(p, "cflags")) { |
| 1784 |
cli_buildopt_cflags(bopt, t); |
| 1785 |
} else if (!strcasecmp(p, "cxxflags")) { |
| 1786 |
cli_buildopt_cxxflags(bopt, t); |
| 1787 |
} else if (!strcasecmp(p, "ldflags")) { |
| 1788 |
cli_buildopt_ldflags(bopt, t); |
| 1789 |
} else if (!strcasecmp(p, "single_binary")) { |
| 1790 |
cli_buildopt_single_binary(bopt, t); |
| 1791 |
} else if (!strcasecmp(p, "kore_source")) { |
| 1792 |
cli_buildopt_kore_source(bopt, t); |
| 1793 |
} else if (!strcasecmp(p, "kore_flavor")) { |
| 1794 |
cli_buildopt_kore_flavor(bopt, t); |
| 1795 |
} else if (!strcasecmp(p, "mime_add")) { |
| 1796 |
cli_buildopt_mime(bopt, t); |
| 1797 |
} else { |
| 1798 |
printf("ignoring unknown option '%s'\n", p); |
| 1799 |
} |
| 1800 |
} |
| 1801 |
|
| 1802 |
fclose(fp); |
| 1803 |
|
| 1804 |
if ((env = getenv("KORE_SOURCE")) != NULL) |
| 1805 |
cli_buildopt_kore_source(NULL, env); |
| 1806 |
|
| 1807 |
if ((env = getenv("KORE_FLAVOR")) != NULL) |
| 1808 |
cli_buildopt_kore_flavor(NULL, env); |
| 1809 |
} |
| 1810 |
|
| 1811 |
static struct buildopt * |
| 1812 |
cli_buildopt_new(const char *name) |
| 1813 |
{ |
| 1814 |
struct buildopt *bopt; |
| 1815 |
|
| 1816 |
bopt = cli_malloc(sizeof(*bopt)); |
| 1817 |
bopt->cflags = NULL; |
| 1818 |
bopt->cxxflags = NULL; |
| 1819 |
bopt->ldflags = NULL; |
| 1820 |
bopt->flavor_nohttp = 0; |
| 1821 |
bopt->single_binary = 0; |
| 1822 |
bopt->kore_flavor = NULL; |
| 1823 |
bopt->name = cli_strdup(name); |
| 1824 |
|
| 1825 |
(void)cli_vasprintf(&bopt->kore_source, "%s/share/kore/", prefix); |
| 1826 |
|
| 1827 |
TAILQ_INSERT_TAIL(&build_options, bopt, list); |
| 1828 |
return (bopt); |
| 1829 |
} |
| 1830 |
|
| 1831 |
static struct buildopt * |
| 1832 |
cli_buildopt_find(const char *name) |
| 1833 |
{ |
| 1834 |
struct buildopt *bopt; |
| 1835 |
|
| 1836 |
TAILQ_FOREACH(bopt, &build_options, list) { |
| 1837 |
if (!strcmp(bopt->name, name)) |
| 1838 |
return (bopt); |
| 1839 |
} |
| 1840 |
|
| 1841 |
return (NULL); |
| 1842 |
} |
| 1843 |
|
| 1844 |
static struct buildopt * |
| 1845 |
cli_buildopt_default(void) |
| 1846 |
{ |
| 1847 |
struct buildopt *bopt; |
| 1848 |
|
| 1849 |
if ((bopt = cli_buildopt_find("_default")) == NULL) |
| 1850 |
fatal("no _default buildopt options"); |
| 1851 |
|
| 1852 |
return (bopt); |
| 1853 |
} |
| 1854 |
|
| 1855 |
static void |
| 1856 |
cli_buildopt_cleanup(void) |
| 1857 |
{ |
| 1858 |
struct buildopt *bopt, *next; |
| 1859 |
struct mime_type *mime, *mnext; |
| 1860 |
|
| 1861 |
for (bopt = TAILQ_FIRST(&build_options); bopt != NULL; bopt = next) { |
| 1862 |
next = TAILQ_NEXT(bopt, list); |
| 1863 |
TAILQ_REMOVE(&build_options, bopt, list); |
| 1864 |
|
| 1865 |
if (bopt->cflags != NULL) |
| 1866 |
cli_buf_free(bopt->cflags); |
| 1867 |
if (bopt->cxxflags != NULL) |
| 1868 |
cli_buf_free(bopt->cxxflags); |
| 1869 |
if (bopt->ldflags != NULL) |
| 1870 |
cli_buf_free(bopt->ldflags); |
| 1871 |
if (bopt->kore_source != NULL) |
| 1872 |
free(bopt->kore_source); |
| 1873 |
if (bopt->kore_flavor != NULL) |
| 1874 |
free(bopt->kore_flavor); |
| 1875 |
free(bopt); |
| 1876 |
} |
| 1877 |
|
| 1878 |
for (mime = TAILQ_FIRST(&mime_types); mime != NULL; mime = mnext) { |
| 1879 |
mnext = TAILQ_NEXT(mime, list); |
| 1880 |
TAILQ_REMOVE(&mime_types, mime, list); |
| 1881 |
free(mime->type); |
| 1882 |
free(mime->ext); |
| 1883 |
free(mime); |
| 1884 |
} |
| 1885 |
} |
| 1886 |
|
| 1887 |
static void |
| 1888 |
cli_buildopt_cflags(struct buildopt *bopt, const char *string) |
| 1889 |
{ |
| 1890 |
if (bopt == NULL) |
| 1891 |
bopt = cli_buildopt_default(); |
| 1892 |
|
| 1893 |
if (bopt->cflags == NULL) |
| 1894 |
bopt->cflags = cli_buf_alloc(128); |
| 1895 |
|
| 1896 |
cli_buf_appendf(bopt->cflags, "%s ", string); |
| 1897 |
} |
| 1898 |
|
| 1899 |
static void |
| 1900 |
cli_buildopt_cxxflags(struct buildopt *bopt, const char *string) |
| 1901 |
{ |
| 1902 |
if (bopt == NULL) |
| 1903 |
bopt = cli_buildopt_default(); |
| 1904 |
|
| 1905 |
if (bopt->cxxflags == NULL) |
| 1906 |
bopt->cxxflags = cli_buf_alloc(128); |
| 1907 |
|
| 1908 |
cli_buf_appendf(bopt->cxxflags, "%s ", string); |
| 1909 |
} |
| 1910 |
|
| 1911 |
static void |
| 1912 |
cli_buildopt_ldflags(struct buildopt *bopt, const char *string) |
| 1913 |
{ |
| 1914 |
if (bopt == NULL) |
| 1915 |
bopt = cli_buildopt_default(); |
| 1916 |
|
| 1917 |
if (bopt->ldflags == NULL) |
| 1918 |
bopt->ldflags = cli_buf_alloc(128); |
| 1919 |
|
| 1920 |
cli_buf_appendf(bopt->ldflags, "%s ", string); |
| 1921 |
} |
| 1922 |
|
| 1923 |
static void |
| 1924 |
cli_buildopt_single_binary(struct buildopt *bopt, const char *string) |
| 1925 |
{ |
| 1926 |
if (bopt == NULL) |
| 1927 |
bopt = cli_buildopt_default(); |
| 1928 |
else |
| 1929 |
fatal("single_binary only supported in global context"); |
| 1930 |
|
| 1931 |
if (!strcmp(string, "yes")) |
| 1932 |
bopt->single_binary = 1; |
| 1933 |
else |
| 1934 |
bopt->single_binary = 0; |
| 1935 |
} |
| 1936 |
|
| 1937 |
static void |
| 1938 |
cli_buildopt_kore_source(struct buildopt *bopt, const char *string) |
| 1939 |
{ |
| 1940 |
if (bopt == NULL) |
| 1941 |
bopt = cli_buildopt_default(); |
| 1942 |
else |
| 1943 |
fatal("kore_source only supported in global context"); |
| 1944 |
|
| 1945 |
if (bopt->kore_source != NULL) |
| 1946 |
free(bopt->kore_source); |
| 1947 |
|
| 1948 |
bopt->kore_source = cli_strdup(string); |
| 1949 |
} |
| 1950 |
|
| 1951 |
static void |
| 1952 |
cli_buildopt_kore_flavor(struct buildopt *bopt, const char *string) |
| 1953 |
{ |
| 1954 |
int cnt, i; |
| 1955 |
char *p, *copy, *flavors[10]; |
| 1956 |
|
| 1957 |
if (bopt == NULL) |
| 1958 |
bopt = cli_buildopt_default(); |
| 1959 |
else |
| 1960 |
fatal("kore_flavor only supported in global context"); |
| 1961 |
|
| 1962 |
if (bopt->kore_flavor != NULL) |
| 1963 |
free(bopt->kore_flavor); |
| 1964 |
|
| 1965 |
copy = cli_strdup(string); |
| 1966 |
cnt = cli_split_string(copy, " ", flavors, 10); |
| 1967 |
|
| 1968 |
for (i = 0; i < cnt; i++) { |
| 1969 |
if ((p = strchr(flavors[i], '=')) == NULL) |
| 1970 |
fatal("invalid flavor %s", string); |
| 1971 |
|
| 1972 |
*p = '\0'; |
| 1973 |
|
| 1974 |
if (!strcmp(flavors[i], "NOHTTP")) |
| 1975 |
bopt->flavor_nohttp = 1; |
| 1976 |
} |
| 1977 |
|
| 1978 |
bopt->kore_flavor = cli_strdup(string); |
| 1979 |
free(copy); |
| 1980 |
} |
| 1981 |
|
| 1982 |
static void |
| 1983 |
cli_buildopt_mime(struct buildopt *bopt, const char *ext) |
| 1984 |
{ |
| 1985 |
struct mime_type *mime; |
| 1986 |
char *type; |
| 1987 |
|
| 1988 |
if (bopt == NULL) |
| 1989 |
bopt = cli_buildopt_default(); |
| 1990 |
else |
| 1991 |
fatal("mime_add only supported in global context"); |
| 1992 |
|
| 1993 |
if ((type = strchr(ext, ':')) == NULL) |
| 1994 |
fatal("no type given in %s", ext); |
| 1995 |
|
| 1996 |
*(type)++ = '\0'; |
| 1997 |
TAILQ_FOREACH(mime, &mime_types, list) { |
| 1998 |
if (!strcmp(mime->ext, ext)) |
| 1999 |
fatal("duplicate extension %s found", ext); |
| 2000 |
} |
| 2001 |
|
| 2002 |
mime = cli_malloc(sizeof(*mime)); |
| 2003 |
mime->ext = cli_strdup(ext); |
| 2004 |
mime->type = cli_strdup(type); |
| 2005 |
|
| 2006 |
TAILQ_INSERT_TAIL(&mime_types, mime, list); |
| 2007 |
} |
| 2008 |
|
| 2009 |
static void |
| 2010 |
cli_build_flags_common(struct buildopt *bopt, struct cli_buf *buf) |
| 2011 |
{ |
| 2012 |
size_t len; |
| 2013 |
char *data; |
| 2014 |
|
| 2015 |
cli_buf_appendf(buf, "-fPIC "); |
| 2016 |
|
| 2017 |
if (bopt != NULL) |
| 2018 |
cli_buf_appendf(buf, "-Isrc -Isrc/includes "); |
| 2019 |
|
| 2020 |
if (bopt == NULL || bopt->single_binary == 0) |
| 2021 |
cli_buf_appendf(buf, "-I%s/include ", prefix); |
| 2022 |
else |
| 2023 |
cli_buf_appendf(buf, "-I%s/include ", bopt->kore_source); |
| 2024 |
|
| 2025 |
if (bopt == NULL || bopt->single_binary == 0) { |
| 2026 |
cli_kore_load_file("features", bopt, &data, &len); |
| 2027 |
cli_buf_append(buf, data, len); |
| 2028 |
cli_buf_appendf(buf, " "); |
| 2029 |
free(data); |
| 2030 |
} |
| 2031 |
} |
| 2032 |
|
| 2033 |
static void |
| 2034 |
cli_build_cflags(struct buildopt *bopt) |
| 2035 |
{ |
| 2036 |
size_t len; |
| 2037 |
struct buildopt *obopt; |
| 2038 |
char *string, *buf, *env; |
| 2039 |
|
| 2040 |
if ((obopt = cli_buildopt_find(flavor)) == NULL) |
| 2041 |
fatal("no such build flavor: %s", flavor); |
| 2042 |
|
| 2043 |
if (bopt->cflags == NULL) |
| 2044 |
bopt->cflags = cli_buf_alloc(128); |
| 2045 |
|
| 2046 |
cli_build_flags_common(bopt, bopt->cflags); |
| 2047 |
|
| 2048 |
if (obopt != NULL && obopt->cflags != NULL) { |
| 2049 |
cli_buf_append(bopt->cflags, obopt->cflags->data, |
| 2050 |
obopt->cflags->offset); |
| 2051 |
} |
| 2052 |
|
| 2053 |
if (bopt->single_binary) { |
| 2054 |
cli_kore_load_file("features", bopt, &buf, &len); |
| 2055 |
cli_buf_append(bopt->cflags, buf, len); |
| 2056 |
cli_buf_appendf(bopt->cflags, " "); |
| 2057 |
free(buf); |
| 2058 |
} |
| 2059 |
|
| 2060 |
if ((env = getenv("CFLAGS")) != NULL) |
| 2061 |
cli_buf_appendf(bopt->cflags, "%s", env); |
| 2062 |
|
| 2063 |
string = cli_buf_stringify(bopt->cflags, NULL); |
| 2064 |
printf("CFLAGS=%s\n", string); |
| 2065 |
cflags_count = cli_split_string(string, " ", cflags, CFLAGS_MAX); |
| 2066 |
} |
| 2067 |
|
| 2068 |
static void |
| 2069 |
cli_build_cxxflags(struct buildopt *bopt) |
| 2070 |
{ |
| 2071 |
struct buildopt *obopt; |
| 2072 |
char *string, *env; |
| 2073 |
|
| 2074 |
if ((obopt = cli_buildopt_find(flavor)) == NULL) |
| 2075 |
fatal("no such build flavor: %s", flavor); |
| 2076 |
|
| 2077 |
if (bopt->cxxflags == NULL) |
| 2078 |
bopt->cxxflags = cli_buf_alloc(128); |
| 2079 |
|
| 2080 |
cli_build_flags_common(bopt, bopt->cxxflags); |
| 2081 |
|
| 2082 |
if (obopt != NULL && obopt->cxxflags != NULL) { |
| 2083 |
cli_buf_append(bopt->cxxflags, obopt->cxxflags->data, |
| 2084 |
obopt->cxxflags->offset); |
| 2085 |
} |
| 2086 |
|
| 2087 |
if ((env = getenv("CXXFLAGS")) != NULL) |
| 2088 |
cli_buf_appendf(bopt->cxxflags, "%s", env); |
| 2089 |
|
| 2090 |
string = cli_buf_stringify(bopt->cxxflags, NULL); |
| 2091 |
if (cxx_files_count > 0) |
| 2092 |
printf("CXXFLAGS=%s\n", string); |
| 2093 |
cxxflags_count = cli_split_string(string, " ", cxxflags, CXXFLAGS_MAX); |
| 2094 |
} |
| 2095 |
|
| 2096 |
static void |
| 2097 |
cli_build_ldflags(struct buildopt *bopt) |
| 2098 |
{ |
| 2099 |
int fd; |
| 2100 |
size_t len; |
| 2101 |
struct buildopt *obopt; |
| 2102 |
char *string, *buf, *env, *path; |
| 2103 |
|
| 2104 |
if ((obopt = cli_buildopt_find(flavor)) == NULL) |
| 2105 |
fatal("no such build flavor: %s", flavor); |
| 2106 |
|
| 2107 |
if (bopt->ldflags == NULL) |
| 2108 |
bopt->ldflags = cli_buf_alloc(128); |
| 2109 |
|
| 2110 |
if (bopt->single_binary == 0) { |
| 2111 |
#if defined(__MACH__) |
| 2112 |
cli_buf_appendf(bopt->ldflags, |
| 2113 |
"-dynamiclib -undefined suppress -flat_namespace "); |
| 2114 |
#else |
| 2115 |
cli_buf_appendf(bopt->ldflags, "-shared "); |
| 2116 |
#endif |
| 2117 |
} else { |
| 2118 |
(void)cli_vasprintf(&path, "%s/ldflags", object_dir); |
| 2119 |
cli_file_open(path, O_RDONLY, &fd); |
| 2120 |
cli_file_read(fd, &buf, &len); |
| 2121 |
cli_file_close(fd); |
| 2122 |
if (len == 0) |
| 2123 |
fatal("ldflags is empty"); |
| 2124 |
len--; |
| 2125 |
|
| 2126 |
cli_buf_append(bopt->ldflags, buf, len); |
| 2127 |
cli_buf_appendf(bopt->ldflags, " "); |
| 2128 |
free(buf); |
| 2129 |
} |
| 2130 |
|
| 2131 |
if (obopt != NULL && obopt->ldflags != NULL) { |
| 2132 |
cli_buf_append(bopt->ldflags, obopt->ldflags->data, |
| 2133 |
obopt->ldflags->offset); |
| 2134 |
} |
| 2135 |
|
| 2136 |
if ((env = getenv("LDFLAGS")) != NULL) |
| 2137 |
cli_buf_appendf(bopt->ldflags, "%s", env); |
| 2138 |
|
| 2139 |
string = cli_buf_stringify(bopt->ldflags, NULL); |
| 2140 |
printf("LDFLAGS=%s\n", string); |
| 2141 |
ldflags_count = cli_split_string(string, " ", ldflags, LD_FLAGS_MAX); |
| 2142 |
} |
| 2143 |
|
| 2144 |
static void |
| 2145 |
cli_flavor_load(void) |
| 2146 |
{ |
| 2147 |
FILE *fp; |
| 2148 |
char buf[BUFSIZ], pwd[MAXPATHLEN], *p, *conf, *env; |
| 2149 |
|
| 2150 |
if ((env = getenv("KORE_BUILD_FLAVOR")) != NULL) { |
| 2151 |
flavor = cli_strdup(env); |
| 2152 |
return; |
| 2153 |
} |
| 2154 |
|
| 2155 |
if (getcwd(pwd, sizeof(pwd)) == NULL) |
| 2156 |
fatal("could not get cwd: %s", errno_s); |
| 2157 |
|
| 2158 |
appl = basename(pwd); |
| 2159 |
if (appl == NULL) |
| 2160 |
fatal("basename: %s", errno_s); |
| 2161 |
appl = cli_strdup(appl); |
| 2162 |
(void)cli_vasprintf(&conf, "conf/%s.conf", appl); |
| 2163 |
|
| 2164 |
if (!cli_dir_exists("conf") || !cli_file_exists(conf)) |
| 2165 |
fatal("%s doesn't appear to be a kore app", appl); |
| 2166 |
free(conf); |
| 2167 |
|
| 2168 |
if ((fp = fopen(".flavor", "r")) == NULL) { |
| 2169 |
flavor = cli_strdup("dev"); |
| 2170 |
return; |
| 2171 |
} |
| 2172 |
|
| 2173 |
if (fgets(buf, sizeof(buf), fp) == NULL) |
| 2174 |
fatal("failed to read flavor from file"); |
| 2175 |
|
| 2176 |
if ((p = strchr(buf, '\n')) != NULL) |
| 2177 |
*p = '\0'; |
| 2178 |
|
| 2179 |
flavor = cli_strdup(buf); |
| 2180 |
(void)fclose(fp); |
| 2181 |
} |
| 2182 |
|
| 2183 |
static void |
| 2184 |
cli_kore_load_file(const char *name, struct buildopt *bopt, |
| 2185 |
char **out, size_t *outlen) |
| 2186 |
{ |
| 2187 |
int fd; |
| 2188 |
size_t len; |
| 2189 |
char *path, *data; |
| 2190 |
|
| 2191 |
if (bopt != NULL && bopt->single_binary) { |
| 2192 |
(void)cli_vasprintf(&path, "%s/%s", object_dir, name); |
| 2193 |
} else { |
| 2194 |
(void)cli_vasprintf(&path, "%s/share/kore/%s", prefix, name); |
| 2195 |
} |
| 2196 |
|
| 2197 |
cli_file_open(path, O_RDONLY, &fd); |
| 2198 |
cli_file_read(fd, &data, &len); |
| 2199 |
cli_file_close(fd); |
| 2200 |
free(path); |
| 2201 |
|
| 2202 |
if (len == 0) |
| 2203 |
fatal("%s is empty", name); |
| 2204 |
|
| 2205 |
len--; |
| 2206 |
|
| 2207 |
*out = data; |
| 2208 |
*outlen = len; |
| 2209 |
} |
| 2210 |
|
| 2211 |
static void |
| 2212 |
cli_flavor_change(const char *name) |
| 2213 |
{ |
| 2214 |
FILE *fp; |
| 2215 |
int ret; |
| 2216 |
struct buildopt *bopt; |
| 2217 |
|
| 2218 |
if ((bopt = cli_buildopt_find(name)) == NULL) |
| 2219 |
fatal("no such flavor: %s", name); |
| 2220 |
|
| 2221 |
if ((fp = fopen(".flavor.tmp", "w")) == NULL) |
| 2222 |
fatal("failed to open temporary file to save flavor"); |
| 2223 |
|
| 2224 |
ret = fprintf(fp, "%s\n", name); |
| 2225 |
if (ret == -1 || (size_t)ret != (strlen(name) + 1)) |
| 2226 |
fatal("failed to write new build flavor"); |
| 2227 |
|
| 2228 |
(void)fclose(fp); |
| 2229 |
|
| 2230 |
if (rename(".flavor.tmp", ".flavor") == -1) |
| 2231 |
fatal("failed to replace build flavor"); |
| 2232 |
|
| 2233 |
cli_clean(0, NULL); |
| 2234 |
} |
| 2235 |
|
| 2236 |
static void |
| 2237 |
cli_spawn_proc(void (*cb)(void *), void *arg) |
| 2238 |
{ |
| 2239 |
pid_t pid; |
| 2240 |
int status; |
| 2241 |
|
| 2242 |
pid = fork(); |
| 2243 |
switch (pid) { |
| 2244 |
case -1: |
| 2245 |
fatal("cli_compile_cfile: fork() %s", errno_s); |
| 2246 |
/* NOTREACHED */ |
| 2247 |
case 0: |
| 2248 |
cb(arg); |
| 2249 |
fatal("cli_spawn_proc: %s", errno_s); |
| 2250 |
/* NOTREACHED */ |
| 2251 |
default: |
| 2252 |
break; |
| 2253 |
} |
| 2254 |
|
| 2255 |
if (waitpid(pid, &status, 0) == -1) |
| 2256 |
fatal("couldn't wait for child %d", pid); |
| 2257 |
|
| 2258 |
if (WEXITSTATUS(status) || WTERMSIG(status) || WCOREDUMP(status)) |
| 2259 |
fatal("subprocess trouble, check output"); |
| 2260 |
} |
| 2261 |
|
| 2262 |
static int |
| 2263 |
cli_vasprintf(char **out, const char *fmt, ...) |
| 2264 |
{ |
| 2265 |
int l; |
| 2266 |
va_list args; |
| 2267 |
|
| 2268 |
va_start(args, fmt); |
| 2269 |
l = vasprintf(out, fmt, args); |
| 2270 |
va_end(args); |
| 2271 |
|
| 2272 |
if (l == -1) |
| 2273 |
fatal("cli_vasprintf"); |
| 2274 |
|
| 2275 |
return (l); |
| 2276 |
} |
| 2277 |
|
| 2278 |
static void |
| 2279 |
cli_cleanup_files(const char *spath) |
| 2280 |
{ |
| 2281 |
cli_find_files(spath, cli_file_remove); |
| 2282 |
|
| 2283 |
if (rmdir(spath) == -1 && errno != ENOENT) |
| 2284 |
printf("couldn't rmdir %s\n", spath); |
| 2285 |
} |
| 2286 |
|
| 2287 |
static void * |
| 2288 |
cli_malloc(size_t len) |
| 2289 |
{ |
| 2290 |
void *ptr; |
| 2291 |
|
| 2292 |
if ((ptr = calloc(1, len)) == NULL) |
| 2293 |
fatal("calloc: %s", errno_s); |
| 2294 |
|
| 2295 |
return (ptr); |
| 2296 |
} |
| 2297 |
|
| 2298 |
static void * |
| 2299 |
cli_realloc(void *ptr, size_t len) |
| 2300 |
{ |
| 2301 |
void *nptr; |
| 2302 |
|
| 2303 |
if ((nptr = realloc(ptr, len)) == NULL) |
| 2304 |
fatal("realloc: %s", errno_s); |
| 2305 |
|
| 2306 |
return (nptr); |
| 2307 |
} |
| 2308 |
|
| 2309 |
static char * |
| 2310 |
cli_strdup(const char *string) |
| 2311 |
{ |
| 2312 |
char *copy; |
| 2313 |
|
| 2314 |
if ((copy = strdup(string)) == NULL) |
| 2315 |
fatal("strdup: %s", errno_s); |
| 2316 |
|
| 2317 |
return (copy); |
| 2318 |
} |
| 2319 |
|
| 2320 |
struct cli_buf * |
| 2321 |
cli_buf_alloc(size_t initial) |
| 2322 |
{ |
| 2323 |
struct cli_buf *buf; |
| 2324 |
|
| 2325 |
buf = cli_malloc(sizeof(*buf)); |
| 2326 |
|
| 2327 |
if (initial > 0) |
| 2328 |
buf->data = cli_malloc(initial); |
| 2329 |
else |
| 2330 |
buf->data = NULL; |
| 2331 |
|
| 2332 |
buf->length = initial; |
| 2333 |
buf->offset = 0; |
| 2334 |
|
| 2335 |
return (buf); |
| 2336 |
} |
| 2337 |
|
| 2338 |
void |
| 2339 |
cli_buf_free(struct cli_buf *buf) |
| 2340 |
{ |
| 2341 |
free(buf->data); |
| 2342 |
buf->data = NULL; |
| 2343 |
buf->offset = 0; |
| 2344 |
buf->length = 0; |
| 2345 |
free(buf); |
| 2346 |
} |
| 2347 |
|
| 2348 |
void |
| 2349 |
cli_buf_append(struct cli_buf *buf, const void *d, size_t len) |
| 2350 |
{ |
| 2351 |
if ((buf->offset + len) < len) |
| 2352 |
fatal("overflow in cli_buf_append"); |
| 2353 |
|
| 2354 |
if ((buf->offset + len) > buf->length) { |
| 2355 |
buf->length += len; |
| 2356 |
buf->data = cli_realloc(buf->data, buf->length); |
| 2357 |
} |
| 2358 |
|
| 2359 |
memcpy((buf->data + buf->offset), d, len); |
| 2360 |
buf->offset += len; |
| 2361 |
} |
| 2362 |
|
| 2363 |
void |
| 2364 |
cli_buf_appendv(struct cli_buf *buf, const char *fmt, va_list args) |
| 2365 |
{ |
| 2366 |
int l; |
| 2367 |
va_list copy; |
| 2368 |
char *b, sb[BUFSIZ]; |
| 2369 |
|
| 2370 |
va_copy(copy, args); |
| 2371 |
|
| 2372 |
l = vsnprintf(sb, sizeof(sb), fmt, args); |
| 2373 |
if (l == -1) |
| 2374 |
fatal("cli_buf_appendv(): vsnprintf error"); |
| 2375 |
|
| 2376 |
if ((size_t)l >= sizeof(sb)) { |
| 2377 |
l = vasprintf(&b, fmt, copy); |
| 2378 |
if (l == -1) |
| 2379 |
fatal("cli_buf_appendv(): error or truncation"); |
| 2380 |
} else { |
| 2381 |
b = sb; |
| 2382 |
} |
| 2383 |
|
| 2384 |
cli_buf_append(buf, b, l); |
| 2385 |
if (b != sb) |
| 2386 |
free(b); |
| 2387 |
|
| 2388 |
va_end(copy); |
| 2389 |
} |
| 2390 |
|
| 2391 |
void |
| 2392 |
cli_buf_appendf(struct cli_buf *buf, const char *fmt, ...) |
| 2393 |
{ |
| 2394 |
va_list args; |
| 2395 |
|
| 2396 |
va_start(args, fmt); |
| 2397 |
cli_buf_appendv(buf, fmt, args); |
| 2398 |
va_end(args); |
| 2399 |
} |
| 2400 |
|
| 2401 |
char * |
| 2402 |
cli_buf_stringify(struct cli_buf *buf, size_t *len) |
| 2403 |
{ |
| 2404 |
char c; |
| 2405 |
|
| 2406 |
if (len != NULL) |
| 2407 |
*len = buf->offset; |
| 2408 |
|
| 2409 |
c = '\0'; |
| 2410 |
cli_buf_append(buf, &c, sizeof(c)); |
| 2411 |
|
| 2412 |
return ((char *)buf->data); |
| 2413 |
} |
| 2414 |
|
| 2415 |
static int |
| 2416 |
cli_split_string(char *input, const char *delim, char **out, size_t ele) |
| 2417 |
{ |
| 2418 |
int count; |
| 2419 |
char **ap; |
| 2420 |
|
| 2421 |
if (ele == 0) |
| 2422 |
return (0); |
| 2423 |
|
| 2424 |
count = 0; |
| 2425 |
for (ap = out; ap < &out[ele - 1] && |
| 2426 |
(*ap = strsep(&input, delim)) != NULL;) { |
| 2427 |
if (**ap != '\0') { |
| 2428 |
ap++; |
| 2429 |
count++; |
| 2430 |
} |
| 2431 |
} |
| 2432 |
|
| 2433 |
*ap = NULL; |
| 2434 |
return (count); |
| 2435 |
} |
| 2436 |
|
| 2437 |
static char * |
| 2438 |
cli_read_line(FILE *fp, char *in, size_t len) |
| 2439 |
{ |
| 2440 |
char *p, *t; |
| 2441 |
|
| 2442 |
if (fgets(in, len, fp) == NULL) |
| 2443 |
return (NULL); |
| 2444 |
|
| 2445 |
p = in; |
| 2446 |
in[strcspn(in, "\n")] = '\0'; |
| 2447 |
|
| 2448 |
while (isspace(*(unsigned char *)p)) |
| 2449 |
p++; |
| 2450 |
|
| 2451 |
if (p[0] == '#' || p[0] == '\0') { |
| 2452 |
p[0] = '\0'; |
| 2453 |
return (p); |
| 2454 |
} |
| 2455 |
|
| 2456 |
for (t = p; *t != '\0'; t++) { |
| 2457 |
if (*t == '\t') |
| 2458 |
*t = ' '; |
| 2459 |
} |
| 2460 |
|
| 2461 |
return (p); |
| 2462 |
} |
| 2463 |
|
| 2464 |
static char * |
| 2465 |
cli_text_trim(char *string, size_t len) |
| 2466 |
{ |
| 2467 |
char *end; |
| 2468 |
|
| 2469 |
if (len == 0) |
| 2470 |
return (string); |
| 2471 |
|
| 2472 |
end = (string + len) - 1; |
| 2473 |
while (isspace(*(unsigned char *)string) && string < end) |
| 2474 |
string++; |
| 2475 |
|
| 2476 |
while (isspace(*(unsigned char *)end) && end > string) |
| 2477 |
*(end)-- = '\0'; |
| 2478 |
|
| 2479 |
return (string); |
| 2480 |
} |
| 2481 |
|
| 2482 |
static long long |
| 2483 |
cli_strtonum(const char *str, long long min, long long max) |
| 2484 |
{ |
| 2485 |
long long l; |
| 2486 |
char *ep; |
| 2487 |
|
| 2488 |
if (min > max) |
| 2489 |
fatal("cli_strtonum: min > max"); |
| 2490 |
|
| 2491 |
errno = 0; |
| 2492 |
l = strtoll(str, &ep, 10); |
| 2493 |
if (errno != 0 || str == ep || *ep != '\0') |
| 2494 |
fatal("strtoll(): %s", errno_s); |
| 2495 |
|
| 2496 |
if (l < min) |
| 2497 |
fatal("cli_strtonum: value < min"); |
| 2498 |
|
| 2499 |
if (l > max) |
| 2500 |
fatal("cli_strtonum: value > max"); |
| 2501 |
|
| 2502 |
return (l); |
| 2503 |
} |
| 2504 |
|
| 2505 |
static void |
| 2506 |
fatal(const char *fmt, ...) |
| 2507 |
{ |
| 2508 |
va_list args; |
| 2509 |
char buf[2048]; |
| 2510 |
|
| 2511 |
va_start(args, fmt); |
| 2512 |
(void)vsnprintf(buf, sizeof(buf), fmt, args); |
| 2513 |
va_end(args); |
| 2514 |
|
| 2515 |
if (command != NULL) |
| 2516 |
printf("kore %s: %s\n", command->name, buf); |
| 2517 |
else |
| 2518 |
printf("kore: %s\n", buf); |
| 2519 |
|
| 2520 |
exit(1); |
| 2521 |
} |