From f1e534022dbbd6004ab44cbc0f8c03f1d946deba Mon Sep 17 00:00:00 2001 From: Martin Quinson Date: Mon, 7 Mar 2016 09:43:43 +0100 Subject: [PATCH 1/1] cmake: More cleanups of the sysv contexts I do several commits because this part is sensible and I want to check the intermediate steps on the autobuilders. --- CMakeLists.txt | 136 +++++++++--------- tools/cmake/DefinePackages.cmake | 2 +- ...rog_AC_CHECK_MCSC.c => prog_makecontext.c} | 33 ++--- 3 files changed, 78 insertions(+), 93 deletions(-) rename tools/cmake/test_prog/{prog_AC_CHECK_MCSC.c => prog_makecontext.c} (64%) diff --git a/CMakeLists.txt b/CMakeLists.txt index ba37530cf8..3ec3296a3b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -504,23 +504,75 @@ if(HAVE_PTHREAD) endif() -# This is needed for ucontext on MacOS X: -if(CMAKE_SYSTEM_NAME MATCHES "Darwin") - add_definitions(-D_XOPEN_SOURCE=700 -D_DARWIN_C_SOURCE) +if(HAVE_PTHREAD) + set(HAVE_THREAD_CONTEXTS 1) + message("-- Support for thread context factory ok.") endif() -try_compile(HAVE_UCONTEXT + + +if(NOT HAVE_UCONTEXT_H) + message("-- No ucontext factory: not found.") +else() + # This is needed for ucontext on MacOS X: + if(CMAKE_SYSTEM_NAME MATCHES "Darwin") + add_definitions(-D_XOPEN_SOURCE=700 -D_DARWIN_C_SOURCE) + endif() + try_compile(compile_makecontext ${CMAKE_BINARY_DIR} ${CMAKE_HOME_DIRECTORY}/tools/cmake/test_prog/prog_makecontext.c + OUTPUT_VARIABLE compile_makecontext_output) + + #If can have both context + if(compile_makecontext) + set(HAVE_UCONTEXT_CONTEXTS 1) + message("-- Support for ucontext factory ok.") + else() + message("-- Error: exists, but makecontext is not compilable. Compilation output:\n ${compile_makecontext_output}") + message("-- No ucontext factory: makecontext() is not compilable.") + endif() + + # Stack setup (size and address) + try_run(RUN_makecontext_VAR COMPILE_makecontext_VAR + ${CMAKE_BINARY_DIR} ${CMAKE_HOME_DIRECTORY}/tools/cmake/test_prog/prog_stacksetup.c + RUN_OUTPUT_VARIABLE stack_setup) + + LIST(LENGTH stack_setup stack_setup_len) + if("${stack_setup_len}" STREQUAL "2") + LIST(GET stack_setup 0 makecontext_addr) + LIST(GET stack_setup 1 makecontext_size) + set(sg_makecontext_stack_addr "#define sg_makecontext_stack_addr(skaddr) (${makecontext_addr})") + set(sg_makecontext_stack_size "#define sg_makecontext_stack_size(sksize) (${makecontext_size})") + else() + message(FATAL_ERROR "Could not figure out the stack setup. Compil: ${RUN_makecontext_VAR}. Exec: ${COMPILE_makecontext_VAR}. Output: ${stack_setup}") + endif() + + # Stack growth direction (upward or downward) + try_run(RUN_stackgrowth_VAR COMPILE_stackgrowth_VAR ${CMAKE_BINARY_DIR} - ${CMAKE_HOME_DIRECTORY}/tools/cmake/test_prog/prog_AC_CHECK_MCSC.c) + ${CMAKE_HOME_DIRECTORY}/tools/cmake/test_prog/prog_stackgrowth.c + RUN_OUTPUT_VARIABLE stack + COPY_FILE test_stackgrowth) -#If can have both context -if(HAVE_UCONTEXT) - set(HAVE_UCONTEXT_CONTEXTS 1) - message("-- Support for ucontext factory") + if("${stack}" STREQUAL "down") + set(PTH_STACKGROWTH "-1") + elseif("${stack}" STREQUAL "up") + set(PTH_STACKGROWTH "1") + else() + if("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "x86_64") + set(PTH_STACKGROWTH "-1") + elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "i686") + set(PTH_STACKGROWTH "-1") + else() + message(FATAL_ERROR "Could not figure out the stack direction. Test prog returned: ${stack}; CMAKE_SYSTEM_PROCESSOR: ${CMAKE_SYSTEM_PROCESSOR}.") + endif() + endif() + # If the test ran well, remove the test binary + execute_process(COMMAND ${CMAKE_COMMAND} -E remove test_stackgrowth) endif() +#-------------------------------------------------------------------------------------------------- -if(HAVE_PTHREAD) - set(HAVE_THREAD_CONTEXTS 1) - message("-- Support for thread context factory") +### check for addr2line +find_path(ADDR2LINE NAMES addr2line PATHS NO_DEFAULT_PATHS) +if(ADDR2LINE) + set(ADDR2LINE "${ADDR2LINE}/addr2line") endif() ############### @@ -577,66 +629,6 @@ endif() if(GIT_DATE) set(SIMGRID_VERSION_BANNER "${SIMGRID_VERSION_BANNER} (${GIT_DATE})") endif() -#-------------------------------------------------------------------------------------------------- - -if(HAVE_UCONTEXT_H) - if(CMAKE_SYSTEM_NAME MATCHES "Darwin") - set(makecontext_CPPFLAGS "${makecontext_CPPFLAGS} -D_XOPEN_SOURCE=700") - endif() - - file(REMOVE ${CMAKE_BINARY_DIR}/conftestval) - - if(NOT CMAKE_CROSSCOMPILING) - try_run(RUN_makecontext_VAR COMPILE_makecontext_VAR - ${CMAKE_BINARY_DIR} ${CMAKE_HOME_DIRECTORY}/tools/cmake/test_prog/prog_stacksetup.c - RUN_OUTPUT_VARIABLE stack_setup - COMPILE_DEFINITIONS "${makecontext_CPPFLAGS}" - ) - endif() - - LIST(LENGTH stack_setup stack_setup_len) - if("${stack_setup_len}" STREQUAL "2") - LIST(GET stack_setup 0 makecontext_addr) - LIST(GET stack_setup 1 makecontext_size) - set(sg_makecontext_stack_addr "#define sg_makecontext_stack_addr(skaddr) (${makecontext_addr})") - set(sg_makecontext_stack_size "#define sg_makecontext_stack_size(sksize) (${makecontext_size})") - else() - message(FATAL_ERROR "Could not figure out the stack setup. Compil: ${RUN_makecontext_VAR}. Exec: ${COMPILE_makecontext_VAR}. Output: ${stack_setup}") - endif() -endif() - -#-------------------------------------------------------------------------------------------------- - -### check for stackgrowth -if (NOT CMAKE_CROSSCOMPILING) - try_run(RUN_stackgrowth_VAR COMPILE_stackgrowth_VAR - ${CMAKE_BINARY_DIR} - ${CMAKE_HOME_DIRECTORY}/tools/cmake/test_prog/prog_stackgrowth.c - RUN_OUTPUT_VARIABLE stack - COPY_FILE test_stackgrowth - ) -endif() -if("${stack}" STREQUAL "down") - set(PTH_STACKGROWTH "-1") -elseif("${stack}" STREQUAL "up") - set(PTH_STACKGROWTH "1") -else() - if("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "x86_64") - set(PTH_STACKGROWTH "-1") - elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "i686") - set(PTH_STACKGROWTH "-1") - else() - message(FATAL_ERROR "Could not figure out the stack direction. Test prog returned: ${stack}; CMAKE_SYSTEM_PROCESSOR: ${CMAKE_SYSTEM_PROCESSOR}.") - endif() -endif() -# If the test ran well, remove the test binary -execute_process(COMMAND ${CMAKE_COMMAND} -E remove test_stackgrowth) - -### check for addr2line -find_path(ADDR2LINE NAMES addr2line PATHS NO_DEFAULT_PATHS) -if(ADDR2LINE) - set(ADDR2LINE "${ADDR2LINE}/addr2line") -endif() ### Generate the required headers and scripts ############################################# diff --git a/tools/cmake/DefinePackages.cmake b/tools/cmake/DefinePackages.cmake index 921b65d31e..fea800ad9d 100644 --- a/tools/cmake/DefinePackages.cmake +++ b/tools/cmake/DefinePackages.cmake @@ -1130,8 +1130,8 @@ set(CMAKE_SOURCE_FILES tools/cmake/scripts/update_tesh.pl tools/cmake/UnitTesting.cmake tools/cmake/src/internal_config.h.in - tools/cmake/test_prog/prog_AC_CHECK_MCSC.c tools/cmake/test_prog/prog_gnu_dynlinker.c + tools/cmake/test_prog/prog_makecontext.c tools/cmake/test_prog/prog_mutex_timedlock.c tools/cmake/test_prog/prog_sem_init.c tools/cmake/test_prog/prog_sem_open.c diff --git a/tools/cmake/test_prog/prog_AC_CHECK_MCSC.c b/tools/cmake/test_prog/prog_makecontext.c similarity index 64% rename from tools/cmake/test_prog/prog_AC_CHECK_MCSC.c rename to tools/cmake/test_prog/prog_makecontext.c index ef9e426748..a3fffe9c2a 100644 --- a/tools/cmake/test_prog/prog_AC_CHECK_MCSC.c +++ b/tools/cmake/test_prog/prog_makecontext.c @@ -1,9 +1,12 @@ -/* Copyright (c) 2010, 2014. The SimGrid Team. - * All rights reserved. */ +/* Copyright (c) 2010, 2014, 2016. The SimGrid Team. All rights reserved. */ /* This program is free software; you can redistribute it and/or modify it * under the terms of the license (GNU LGPL) which comes with this package. */ +#ifdef __APPLE__ +#define _XOPEN_SOURCE 700 +#endif + #include #include #include @@ -13,26 +16,20 @@ ucontext_t uc_main; static void child(void) { + /* switch back to the main context */ if (swapcontext(&uc_child, &uc_main) != 0) exit(2); } int main(int argc, char *argv[]) { - FILE *fp; - void *stack; - - /* the default is that it fails */ - if ((fp = fopen("conftestval", "w")) == NULL) - exit(3); - fprintf(fp, "no\n"); - fclose(fp); + void *stack = malloc(64 * 1024); /* configure a child user-space context */ - if ((stack = malloc(64 * 1024)) == NULL) - exit(4); + if (stack == NULL) + exit(3); if (getcontext(&uc_child) != 0) - exit(5); + exit(4); uc_child.uc_link = NULL; uc_child.uc_stack.ss_sp = (char *) stack + (32 * 1024); uc_child.uc_stack.ss_size = 32 * 1024; @@ -41,15 +38,11 @@ int main(int argc, char *argv[]) /* switch into the user context */ if (swapcontext(&uc_main, &uc_child) != 0) - exit(6); + exit(5); /* Fine, child came home */ - if ((fp = fopen("conftestval", "w")) == NULL) - exit(7); - fprintf(fp, "yes\n"); - fclose(fp); + printf("yes\n"); /* die successfully */ - exit(0); - return 1; + return 0; } -- 2.20.1