Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

llc only supports one input file #36

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 19 additions & 17 deletions cmake/LLVMIRUtil.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -753,14 +753,26 @@ function(llvmir_attach_obj_target)
list(APPEND IN_FULL_LLVMIR_FILES "${IN_LLVMIR_DIR}/${IN_LLVMIR_FILE}")
endforeach()

set(FULL_OUT_LLVMIR_FILE "${WORK_DIR}/${TRGT}.o")
if(SHORT_NAME)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't SHORT_NAME still useful for the multiple output files generated by the changes in your PR?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my project it worked fine without it, so I assumed that I could just remove it.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please reintroduce it and check that your project still works?
Just because it's not needed in your configuration doesn't mean that wasn't introduced for a reason initially.

set(FULL_OUT_LLVMIR_FILE "${WORK_DIR}/${SHORT_NAME}.o")
endif()
get_filename_component(OUT_LLVMIR_FILE ${FULL_OUT_LLVMIR_FILE} NAME)
foreach(IN_LLVMIR_FILE ${IN_FULL_LLVMIR_FILES})
get_filename_component(OUTFILE ${IN_LLVMIR_FILE} NAME_WE)
set(OUT_LLVMIR_FILE "${OUTFILE}.o")
set(FULL_OUT_LLVMIR_FILE "${WORK_DIR}/${OUT_LLVMIR_FILE}")

list(APPEND OUT_LLVMIR_FILES ${OUT_LLVMIR_FILE})
list(APPEND FULL_OUT_LLVMIR_FILES ${FULL_OUT_LLVMIR_FILE})
add_custom_command(OUTPUT ${FULL_OUT_LLVMIR_FILE}
COMMAND llc
ARGS
-filetype=obj
-relocation-model=pic
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do we need this flag here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added this flag because the compiler complained in my real project that I needed to recompile with -fPIC.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rather not have project-specific options hardcoded like this.
This command handles other args and passes them unaltered using ${LLVMIR_ATTACH_UNPARSED_ARGUMENTS}.

Can you please try your project with defining that flag in your CMakeLists.txt?

-o=${FULL_OUT_LLVMIR_FILE}
${LLVMIR_ATTACH_UNPARSED_ARGUMENTS}
${IN_LLVMIR_FILE}
DEPENDS ${IN_LLVMIR_FILE}
COMMENT "Generating object ${OUT_LLVMIR_FILE}"
VERBATIM)

list(APPEND OUT_LLVMIR_FILES ${OUT_LLVMIR_FILE})
list(APPEND FULL_OUT_LLVMIR_FILES ${FULL_OUT_LLVMIR_FILE})
endforeach()

# setup custom target
add_custom_target(${TRGT} DEPENDS ${FULL_OUT_LLVMIR_FILES})
Expand All @@ -786,16 +798,6 @@ function(llvmir_attach_obj_target)
set_property(TARGET ${TRGT} PROPERTY EXCLUDE_FROM_ALL On)
set_property(TARGET ${TRGT} PROPERTY LLVMIR_SHORT_NAME ${SHORT_NAME})

add_custom_command(OUTPUT ${FULL_OUT_LLVMIR_FILE}
COMMAND llc
ARGS
-filetype=obj
${LLVMIR_ATTACH_UNPARSED_ARGUMENTS}
-o ${FULL_OUT_LLVMIR_FILE} ${IN_FULL_LLVMIR_FILES}
DEPENDS ${IN_FULL_LLVMIR_FILES}
COMMENT "Generating object ${OUT_LLVMIR_FILE}"
VERBATIM)

## postamble
endfunction()

Expand Down
17 changes: 17 additions & 0 deletions example04/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# cmake file

cmake_minimum_required(VERSION 3.9)

find_program(CMAKE_C_COMPILER clang)
find_program(CMAKE_CXX_COMPILER clang++)

project(staticlib)

list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/../cmake")

message(STATUS ${CMAKE_MODULE_PATH})
include(LLVMIRUtil)

add_subdirectory(staticlib)
add_subdirectory(executable)

9 changes: 9 additions & 0 deletions example04/README.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Instrumenting an static library

```sh
mkdir build && cd build
cmake ..
make testlib_instrumented
make main
./executable/main
```
3 changes: 3 additions & 0 deletions example04/executable/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
add_executable(main main.c)
target_include_directories(main PUBLIC "./../staticlib/include")
target_link_libraries(main "${CMAKE_BINARY_DIR}/staticlib/libtestlib_instrumented.a")
8 changes: 8 additions & 0 deletions example04/executable/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include "Test1.h"

int main(int argc, char **argv)
{
puts("Calling Test1 from static lib!");
Test1();
return 0;
}
18 changes: 18 additions & 0 deletions example04/staticlib/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
set(CMAKE_BUILD_TYPE Debug)
# set(LLVM_PASS_PLUGIN "<filepath to llvm plugin pass>")

set(SOURCE_FILES
src/Test1.c
src/Test2.c
src/Test3.c
)

add_library(testlib STATIC ${SOURCE_FILES})
target_include_directories(testlib PUBLIC "./include")
set_target_properties(testlib PROPERTIES LINKER_LANGUAGE C)

llvmir_attach_bc_target(testlib_bc testlib)
llvmir_attach_opt_pass_target(testlib_opt testlib_bc) # -load-pass-plugin=${LLVM_PASS_PLUGIN} -passes=ctrace)

llvmir_attach_obj_target(testlib_obj testlib_opt)
llvmir_attach_library(testlib_instrumented testlib_obj STATIC)
4 changes: 4 additions & 0 deletions example04/staticlib/include/Test1.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#include <stdlib.h>
#include "Test2.h"

void Test1();
4 changes: 4 additions & 0 deletions example04/staticlib/include/Test2.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#include "Test3.h"
#include <stdio.h>

void Test2();
3 changes: 3 additions & 0 deletions example04/staticlib/include/Test3.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#include <stdio.h>

void Test3();
7 changes: 7 additions & 0 deletions example04/staticlib/src/Test1.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include "Test1.h"

void Test1()
{
puts("Test1 called");
Test2();
}
7 changes: 7 additions & 0 deletions example04/staticlib/src/Test2.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include "Test2.h"

void Test2()
{
puts("Test2 called!");
Test3();
}
6 changes: 6 additions & 0 deletions example04/staticlib/src/Test3.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include "Test3.h"

void Test3()
{
puts("Test3 called");
}