22 changes: 22 additions & 0 deletions compiler-rt/test/crt/ctor_dtor.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// RUN: %clang -fno-use-init-array -g -c %s -o %t.o
// RUN: %clang -fno-use-init-array -g -o %t -nostdlib %crt1 %crti %crtbegin %t.o -lc %libgcc %crtend %crtn
// RUN: %run %t 2>&1 | FileCheck %s

#include <stdio.h>

// CHECK: ctor()
// CHECK-NEXT: main()
// CHECK-NEXT: dtor()

void __attribute__((constructor)) ctor() {
printf("ctor()\n");
}

void __attribute__((destructor)) dtor() {
printf("dtor()\n");
}

int main() {
printf("main()\n");
return 0;
}
33 changes: 33 additions & 0 deletions compiler-rt/test/crt/dso_handle.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// RUN: %clangxx -g -DCRT_SHARED -c %s -fPIC -o %tshared.o
// RUN: %clangxx -g -c %s -fPIC -o %t.o
// RUN: %clangxx -g -shared -o %t.so -nostdlib %crti %crtbegin %tshared.o %libstdcxx -lc -lm %libgcc %crtend %crtn
// RUN: %clangxx -g -o %t -nostdlib %crt1 %crti %crtbegin %t.o %libstdcxx -lc -lm %libgcc %t.so %crtend %crtn
// RUN: %run %t 2>&1 | FileCheck %s

#include <stdio.h>

// CHECK: 1
// CHECK-NEXT: ~A()

#ifdef CRT_SHARED
bool G;
void C() {
printf("%d\n", G);
}

struct A {
A() { G = true; }
~A() {
printf("~A()\n");
}
};

A a;
#else
void C();

int main() {
C();
return 0;
}
#endif
78 changes: 78 additions & 0 deletions compiler-rt/test/crt/lit.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# -*- Python -*-

import os
import subprocess

# Setup config name.
config.name = 'CRT' + config.name_suffix

# Setup source root.
config.test_source_root = os.path.dirname(__file__)


def get_library_path(file):
cmd = subprocess.Popen([config.clang.strip(),
config.target_cflags.strip(),
'-print-file-name=%s' % file],
stdout=subprocess.PIPE,
env=config.environment)
if not cmd.stdout:
lit_config.fatal("Couldn't find the library path for '%s'" % file)
dir = cmd.stdout.read().strip()
if sys.platform in ['win32'] and execute_external:
# Don't pass dosish path separator to msys bash.exe.
dir = dir.replace('\\', '/')
# Ensure the result is an ascii string, across Python2.5+ - Python3.
return str(dir.decode('ascii'))


def get_libgcc_file_name():
cmd = subprocess.Popen([config.clang.strip(),
config.target_cflags.strip(),
'-print-libgcc-file-name'],
stdout=subprocess.PIPE,
env=config.environment)
if not cmd.stdout:
lit_config.fatal("Couldn't find the library path for '%s'" % file)
dir = cmd.stdout.read().strip()
if sys.platform in ['win32'] and execute_external:
# Don't pass dosish path separator to msys bash.exe.
dir = dir.replace('\\', '/')
# Ensure the result is an ascii string, across Python2.5+ - Python3.
return str(dir.decode('ascii'))


def build_invocation(compile_flags):
return ' ' + ' '.join([config.clang] + compile_flags) + ' '


# Setup substitutions.
config.substitutions.append(
('%clang ', build_invocation([config.target_cflags])))
config.substitutions.append(
('%clangxx ',
build_invocation(config.cxx_mode_flags + [config.target_cflags])))

base_lib = os.path.join(
config.compiler_rt_libdir, "clang_rt.%%s%s.o" % config.target_suffix)
config.substitutions.append(('%crtbegin', base_lib % "crtbegin"))
config.substitutions.append(('%crtend', base_lib % "crtend"))

config.substitutions.append(
('%crt1', get_library_path('crt1.o')))
config.substitutions.append(
('%crti', get_library_path('crti.o')))
config.substitutions.append(
('%crtn', get_library_path('crtn.o')))

config.substitutions.append(
('%libgcc', get_libgcc_file_name()))

config.substitutions.append(
('%libstdcxx', '-l' + config.sanitizer_cxx_lib.lstrip('lib')))

# Default test suffixes.
config.suffixes = ['.c', '.cc', '.cpp']

if config.host_os not in ['Linux']:
config.unsupported = True
14 changes: 14 additions & 0 deletions compiler-rt/test/crt/lit.site.cfg.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
@LIT_SITE_CFG_IN_HEADER@

# Tool-specific config options.
config.name_suffix = "@CRT_TEST_CONFIG_SUFFIX@"
config.crt_lit_source_dir = "@CRT_LIT_SOURCE_DIR@"
config.target_cflags = "@CRT_TEST_TARGET_CFLAGS@"
config.target_arch = "@CRT_TEST_TARGET_ARCH@"
config.sanitizer_cxx_lib = "@SANITIZER_TEST_CXX_LIBNAME@"

# Load common config for all compiler-rt lit tests
lit_config.load_config(config, "@COMPILER_RT_BINARY_DIR@/test/lit.common.configured")

# Load tool-specific config that would do the real work.
lit_config.load_config(config, "@CRT_LIT_SOURCE_DIR@/lit.cfg")