2 * Copyright (C) 2015, Wang Nan <wangnan0@huawei.com>
3 * Copyright (C) 2015, Huawei Inc.
7 #include <sys/utsname.h>
10 #include "llvm-utils.h"
13 #define CLANG_BPF_CMD_DEFAULT_TEMPLATE \
14 "$CLANG_EXEC -D__KERNEL__ $CLANG_OPTIONS " \
15 "$KERNEL_INC_OPTIONS -Wno-unused-value " \
16 "-Wno-pointer-sign -working-directory " \
17 "$WORKING_DIR -c \"$CLANG_SOURCE\" -target bpf -O2 -o -"
19 struct llvm_param llvm_param = {
20 .clang_path = "clang",
21 .clang_bpf_cmd_template = CLANG_BPF_CMD_DEFAULT_TEMPLATE,
25 .user_set_param = false,
28 int perf_llvm_config(const char *var, const char *value)
30 if (prefixcmp(var, "llvm."))
32 var += sizeof("llvm.") - 1;
34 if (!strcmp(var, "clang-path"))
35 llvm_param.clang_path = strdup(value);
36 else if (!strcmp(var, "clang-bpf-cmd-template"))
37 llvm_param.clang_bpf_cmd_template = strdup(value);
38 else if (!strcmp(var, "clang-opt"))
39 llvm_param.clang_opt = strdup(value);
40 else if (!strcmp(var, "kbuild-dir"))
41 llvm_param.kbuild_dir = strdup(value);
42 else if (!strcmp(var, "kbuild-opts"))
43 llvm_param.kbuild_opts = strdup(value);
46 llvm_param.user_set_param = true;
51 search_program(const char *def, const char *name,
54 char *env, *path, *tmp = NULL;
59 if (def && def[0] != '\0') {
61 if (access(def, F_OK) == 0) {
62 strlcpy(output, def, PATH_MAX);
65 } else if (def[0] != '\0')
77 path = strtok_r(env, ":", &tmp);
79 scnprintf(buf, sizeof(buf), "%s/%s", path, name);
80 if (access(buf, F_OK) == 0) {
81 strlcpy(output, buf, PATH_MAX);
85 path = strtok_r(NULL, ":", &tmp);
92 #define READ_SIZE 4096
94 read_from_pipe(const char *cmd, void **p_buf, size_t *p_read_sz)
99 size_t read_sz = 0, buf_sz = 0;
101 file = popen(cmd, "r");
103 pr_err("ERROR: unable to popen cmd: %s\n",
108 while (!feof(file) && !ferror(file)) {
110 * Make buf_sz always have obe byte extra space so we
111 * can put '\0' there.
113 if (buf_sz - read_sz < READ_SIZE + 1) {
116 buf_sz = read_sz + READ_SIZE + 1;
117 new_buf = realloc(buf, buf_sz);
120 pr_err("ERROR: failed to realloc memory\n");
127 read_sz += fread(buf + read_sz, 1, READ_SIZE, file);
130 if (buf_sz - read_sz < 1) {
131 pr_err("ERROR: internal error\n");
137 pr_err("ERROR: error occurred when reading from pipe: %s\n",
143 err = WEXITSTATUS(pclose(file));
151 * If buf is string, give it terminal '\0' to make our life
152 * easier. If buf is not string, that '\0' is out of space
153 * indicated by read_sz so caller won't even notice it.
155 ((char *)buf)[read_sz] = '\0';
163 *p_read_sz = read_sz;
178 force_set_env(const char *var, const char *value)
181 setenv(var, value, 1);
182 pr_debug("set env: %s=%s\n", var, value);
185 pr_debug("unset env: %s\n", var);
193 " \tLLVM 3.7 or newer is required. Which can be found from http://llvm.org\n"
194 " \tYou may want to try git trunk:\n"
195 " \t\tgit clone http://llvm.org/git/llvm.git\n"
197 " \t\tgit clone http://llvm.org/git/clang.git\n\n"
198 " \tOr fetch the latest clang/llvm 3.7 from pre-built llvm packages for\n"
199 " \tdebian/ubuntu:\n"
200 " \t\thttp://llvm.org/apt\n\n"
201 " \tIf you are using old version of clang, change 'clang-bpf-cmd-template'\n"
202 " \toption in [llvm] section of ~/.perfconfig to:\n\n"
203 " \t \"$CLANG_EXEC $CLANG_OPTIONS $KERNEL_INC_OPTIONS \\\n"
204 " \t -working-directory $WORKING_DIR -c $CLANG_SOURCE \\\n"
205 " \t -emit-llvm -o - | /path/to/llc -march=bpf -filetype=obj -o -\"\n"
206 " \t(Replace /path/to/llc with path to your llc)\n\n"
210 static int detect_kbuild_dir(char **kbuild_dir)
212 const char *test_dir = llvm_param.kbuild_dir;
213 const char *prefix_dir = "";
214 const char *suffix_dir = "";
217 struct utsname utsname;
222 err = uname(&utsname);
224 pr_warning("uname failed: %s\n", strerror(errno));
228 test_dir = utsname.release;
229 prefix_dir = "/lib/modules/";
230 suffix_dir = "/build";
233 err = asprintf(&autoconf_path, "%s%s%s/include/generated/autoconf.h",
234 prefix_dir, test_dir, suffix_dir);
238 if (access(autoconf_path, R_OK) == 0) {
241 err = asprintf(kbuild_dir, "%s%s%s", prefix_dir, test_dir,
251 static const char *kinc_fetch_script =
252 "#!/usr/bin/env sh\n"
253 "if ! test -d \"$KBUILD_DIR\"\n"
257 "if ! test -f \"$KBUILD_DIR/include/generated/autoconf.h\"\n"
261 "TMPDIR=`mktemp -d`\n"
262 "if test -z \"$TMPDIR\"\n"
266 "cat << EOF > $TMPDIR/Makefile\n"
268 "\\$(obj)/%.o: \\$(src)/%.c\n"
269 "\t@echo -n \"\\$(NOSTDINC_FLAGS) \\$(LINUXINCLUDE) \\$(EXTRA_CFLAGS)\"\n"
271 "touch $TMPDIR/dummy.c\n"
272 "make -s -C $KBUILD_DIR M=$TMPDIR $KBUILD_OPTS dummy.o 2>/dev/null\n"
278 get_kbuild_opts(char **kbuild_dir, char **kbuild_include_opts)
282 if (!kbuild_dir || !kbuild_include_opts)
286 *kbuild_include_opts = NULL;
288 if (llvm_param.kbuild_dir && !llvm_param.kbuild_dir[0]) {
289 pr_debug("[llvm.kbuild-dir] is set to \"\" deliberately.\n");
290 pr_debug("Skip kbuild options detection.\n");
294 err = detect_kbuild_dir(kbuild_dir);
297 "WARNING:\tunable to get correct kernel building directory.\n"
298 "Hint:\tSet correct kbuild directory using 'kbuild-dir' option in [llvm]\n"
299 " \tsection of ~/.perfconfig or set it to \"\" to suppress kbuild\n"
300 " \tdetection.\n\n");
304 pr_debug("Kernel build dir is set to %s\n", *kbuild_dir);
305 force_set_env("KBUILD_DIR", *kbuild_dir);
306 force_set_env("KBUILD_OPTS", llvm_param.kbuild_opts);
307 err = read_from_pipe(kinc_fetch_script,
308 (void **)kbuild_include_opts,
312 "WARNING:\tunable to get kernel include directories from '%s'\n"
313 "Hint:\tTry set clang include options using 'clang-bpf-cmd-template'\n"
314 " \toption in [llvm] section of ~/.perfconfig and set 'kbuild-dir'\n"
315 " \toption in [llvm] to \"\" to suppress this detection.\n\n",
323 pr_debug("include option is set to %s\n", *kbuild_include_opts);
326 int llvm__compile_bpf(const char *path, void **p_obj_buf,
327 size_t *p_obj_buf_sz)
330 char clang_path[PATH_MAX];
331 const char *clang_opt = llvm_param.clang_opt;
332 const char *template = llvm_param.clang_bpf_cmd_template;
333 char *kbuild_dir = NULL, *kbuild_include_opts = NULL;
334 void *obj_buf = NULL;
338 template = CLANG_BPF_CMD_DEFAULT_TEMPLATE;
340 err = search_program(llvm_param.clang_path,
341 "clang", clang_path);
344 "ERROR:\tunable to find clang.\n"
345 "Hint:\tTry to install latest clang/llvm to support BPF. Check your $PATH\n"
346 " \tand 'clang-path' option in [llvm] section of ~/.perfconfig.\n");
352 * This is an optional work. Even it fail we can continue our
353 * work. Needn't to check error return.
355 get_kbuild_opts(&kbuild_dir, &kbuild_include_opts);
357 force_set_env("CLANG_EXEC", clang_path);
358 force_set_env("CLANG_OPTIONS", clang_opt);
359 force_set_env("KERNEL_INC_OPTIONS", kbuild_include_opts);
360 force_set_env("WORKING_DIR", kbuild_dir ? : ".");
363 * Since we may reset clang's working dir, path of source file
364 * should be transferred into absolute path, except we want
365 * stdin to be source file (testing).
367 force_set_env("CLANG_SOURCE",
368 (path[0] == '-') ? path :
369 make_nonrelative_path(path));
371 pr_debug("llvm compiling command template: %s\n", template);
372 err = read_from_pipe(template, &obj_buf, &obj_buf_sz);
374 pr_err("ERROR:\tunable to compile %s\n", path);
375 pr_err("Hint:\tCheck error message shown above.\n");
376 pr_err("Hint:\tYou can also pre-compile it into .o using:\n");
377 pr_err(" \t\tclang -target bpf -O2 -c %s\n", path);
378 pr_err(" \twith proper -I and -D options.\n");
383 free(kbuild_include_opts);
387 *p_obj_buf = obj_buf;
390 *p_obj_buf_sz = obj_buf_sz;
394 free(kbuild_include_opts);
403 int llvm__search_clang(void)
405 char clang_path[PATH_MAX];
407 return search_program(llvm_param.clang_path, "clang", clang_path);