Skip to content

Commit

Permalink
clang/loader: Add support for specifying absolute path of kernel sources
Browse files Browse the repository at this point in the history
Many developers have kernel sources at an specific location. Allow
clang to build from there by allowing to specify a single absolute
path to the kernel sources through a new env var BCC_KERNEL_SOURCE.

Signed-off-by: Joel Fernandes <[email protected]>
  • Loading branch information
Joel Fernandes committed Feb 6, 2018
1 parent 3613ff8 commit 11f3a27
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions src/cc/frontends/clang/loader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include <unistd.h>
#include <utility>
#include <vector>
#include <iostream>
#include <linux/bpf.h>

#include <clang/Basic/FileManager.h>
Expand Down Expand Up @@ -108,11 +109,24 @@ int ClangLoader::parse(unique_ptr<llvm::Module> *mod, TableStorage &ts,
unique_ptr<llvm::MemoryBuffer> main_buf;
struct utsname un;
uname(&un);
string kdir = string(KERNEL_MODULES_DIR) + "/" + un.release;
auto kernel_path_info = get_kernel_path_info (kdir);
string kdir, kpath;
const char *kpath_env = ::getenv("BCC_KERNEL_SOURCE");
bool has_kpath_source = false;

if (kpath_env) {
kpath = string(kpath_env);
} else {
kdir = string(KERNEL_MODULES_DIR) + "/" + un.release;
auto kernel_path_info = get_kernel_path_info(kdir);
has_kpath_source = kernel_path_info.first;
kpath = kdir + "/" + kernel_path_info.second;
}

if (flags_ & DEBUG_PREPROCESSOR)
std::cout << "Running from kernel directory at: " << kpath.c_str() << "\n";

// clang needs to run inside the kernel dir
DirStack dstack(kdir + "/" + kernel_path_info.second);
DirStack dstack(kpath);
if (!dstack.ok())
return -1;

Expand Down Expand Up @@ -143,7 +157,8 @@ int ClangLoader::parse(unique_ptr<llvm::Module> *mod, TableStorage &ts,
"-fno-asynchronous-unwind-tables",
"-x", "c", "-c", abs_file.c_str()});

KBuildHelper kbuild_helper(kdir, kernel_path_info.first);
KBuildHelper kbuild_helper(kpath_env ? kpath : kdir, has_kpath_source);

vector<string> kflags;
if (kbuild_helper.get_flags(un.machine, &kflags))
return -1;
Expand Down

0 comments on commit 11f3a27

Please sign in to comment.