aboutsummaryrefslogtreecommitdiff
path: root/tests/CMakeLists.txt
blob: 1b2514e1181b5c75d3e57c888dc414db086aa347 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
find_package(Yasm REQUIRED)

if (CMAKE_SIZEOF_VOID_P EQUAL 4 OR SUBHOOK_FORCE_32BIT)
  set(BITS 32)
else()
  set(BITS 64)
endif()

set(asm_file foo_${BITS}.asm)
if(CMAKE_SIZEOF_VOID_P EQUAL 8 AND NOT SUBHOOK_FORCE_32BIT)
  if(WIN32)
    set(asm_file foo_64_win.asm)
  elseif(UNIX)
    set(asm_file foo_64_unix.asm)
  endif()
endif()

if(WIN32)
  set(asm_format "win${BITS}")
elseif(APPLE)
  set(asm_format "macho${BITS}")
else()
  set(asm_format "elf${BITS}")
endif()

set(options "-f" "${asm_format}")
if(APPLE
    OR (WIN32 AND (CMAKE_SIZEOF_VOID_P EQUAL 4 OR SUBHOOK_FORCE_32BIT)))
  list(APPEND options "--prefix=_")
endif()
if(CMAKE_SYSTEM_NAME MATCHES "Linux")
  list(APPEND options "-DUSE_PLT")
endif()

add_custom_command(
  OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/${asm_file}.obj"
  COMMAND "${YASM_EXECUTABLE}" ${options} "-o"
    "${CMAKE_CURRENT_BINARY_DIR}/${asm_file}.obj"
    "${CMAKE_CURRENT_SOURCE_DIR}/${asm_file}"
  MAIN_DEPENDENCY "${CMAKE_CURRENT_SOURCE_DIR}/${asm_file}"
)

add_executable(subhook_test
  test.c
  "${CMAKE_CURRENT_BINARY_DIR}/${asm_file}.obj"
)

set_target_properties(subhook_test PROPERTIES OUTPUT_NAME test)

if(SUBHOOK_FORCE_32BIT)
  if(APPLE)
    set_target_properties(subhook_test PROPERTIES OSX_ARCHITECTURES i386)
  endif()
  if(UNIX)
    set_property(TARGET subhook_test APPEND_STRING PROPERTY
                 COMPILE_FLAGS " -m32")
    set_property(TARGET subhook_test APPEND_STRING PROPERTY LINK_FLAGS " -m32")
  endif()
endif()

target_link_libraries(subhook_test subhook)

if(MSVC)
  set_property(TARGET subhook_test
               APPEND_STRING PROPERTY LINK_FLAGS " /INCREMENTAL:NO")
endif()

if(APPLE AND CMAKE_SIZEOF_VOID_P EQUAL 8 AND NOT SUBHOOK_FORCE_32BIT)
  set_property(TARGET subhook_test APPEND_STRING PROPERTY
               LINK_FLAGS " -Wl,-no_pie")
endif()

add_test(NAME test COMMAND $<TARGET_FILE:subhook_test>)

set(expected_output "\
Testing initial install[\r\n]+\
foo_hooked\\(\\) called[\r\n]+\
foo\\(\\) called[\r\n]+\
Testing re-install[\r\n]+\
foo_hooked\\(\\) called[\r\n]+\
foo\\(\\) called[\r\n]+\
Testing trampoline[\r\n]+\
foo_hooked_tr\\(\\) called[\r\n]+\
foo\\(\\) called[\r\n]+\
")
set_tests_properties(test PROPERTIES
                     PASS_REGULAR_EXPRESSION "${expected_output}")

if(WIN32 AND NOT SUBHOOK_STATIC)
  set_tests_properties(test PROPERTIES
                       ENVIRONMENT PATH=$<TARGET_FILE_DIR:subhook>)
endif()