CMakeLists.txt 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. set(impls
  2. "devurandom"
  3. )
  4. set(impls_Windows
  5. "CryptGenRandom"
  6. )
  7. if(NOT "${impls_${CMAKE_SYSTEM_NAME}}" STREQUAL "")
  8. list(INSERT impls 0 ${impls_${CMAKE_SYSTEM_NAME}})
  9. endif()
  10. function(success dir impl)
  11. message("randombytes() ${impl} -- Succeeded!")
  12. file(COPY "${dir}/randombytes-impl.h" DESTINATION "${CMAKE_BINARY_DIR}/include_internal")
  13. file(RENAME
  14. ${CMAKE_BINARY_DIR}/include_internal/randombytes-impl.h
  15. ${CMAKE_BINARY_DIR}/include_internal/randombytes.h
  16. )
  17. include_directories(${dir})
  18. add_library(randombytes ${dir}/${impl}.c)
  19. endfunction()
  20. file(READ "${CMAKE_SOURCE_DIR}/randombytes/test.c" testContent)
  21. foreach(impl ${impls})
  22. set(dir "${CMAKE_BINARY_DIR}/randombytes/${impl}")
  23. file(MAKE_DIRECTORY ${dir})
  24. file(COPY "${CMAKE_SOURCE_DIR}/randombytes/${impl}.c" DESTINATION ${dir})
  25. file(RENAME "${dir}/${impl}.c" "${dir}/test.c")
  26. file(APPEND "${dir}/test.c" "${testContent}")
  27. file(COPY "${CMAKE_SOURCE_DIR}/randombytes/${impl}.c" DESTINATION ${dir})
  28. file(COPY "${CMAKE_SOURCE_DIR}/randombytes/${impl}.h" DESTINATION ${dir})
  29. file(RENAME "${dir}/${impl}.h" "${dir}/randombytes-impl.h")
  30. if (CMAKE_CROSSCOMPILING)
  31. try_compile(compileResult
  32. ${dir}
  33. ${dir}/test.c
  34. OUTPUT_VARIABLE compileOut
  35. )
  36. if(NOT compileResult)
  37. message("randombytes() ${impl} -- Failed to compile [${compileOut}]")
  38. else()
  39. success("${dir}" "${impl}")
  40. break()
  41. endif()
  42. else()
  43. try_run(runResult compileResult
  44. ${dir}
  45. ${dir}/test.c
  46. COMPILE_OUTPUT_VARIABLE compileOut
  47. )
  48. if(NOT compileResult)
  49. message("randombytes() ${impl} -- Failed to compile [${compileOut}]")
  50. elseif(runResult)
  51. message("randombytes() ${impl} -- Failed")
  52. else()
  53. success(${dir} ${impl})
  54. break()
  55. endif()
  56. endif()
  57. endforeach()