make port number deterministic

This commit is contained in:
mpilman 2020-01-22 14:04:31 -08:00
parent 92f4c30150
commit 30e4f14d1d

View File

@ -59,8 +59,26 @@ endfunction()
message(STATUS "Add html target")
add_documentation_target(GENERATOR html)
set(DOCSERVER_PORT "-1" CACHE STRING "Port to which the documentation server should bind (negative means cmake will chose one)")
if(DOCSERVER_PORT GREATER_EQUAL 0)
set(port ${DOCSERVER_PORT})
else()
if(WIN32)
set(username $ENV{USERNAME})
else()
set(username $ENV{USER})
endif()
string(MD5 username_hash ${username})
# cmake math function can only use 64 bit signed integers - so we just truncate the string
string(SUBSTRING "${username_hash}" 0 15 username_hash_small)
message(STATUS math(EXPR port "(0x${username_hash_small} % 8000) + 8000" OUTPUT_FORMAT DECIMAL))
math(EXPR port "(0x${username_hash_small} % 8000) + 8000" OUTPUT_FORMAT DECIMAL)
message(STATUS "Port is ${port}")
endif()
add_custom_target(docpreview
COMMAND ${python_command} -m SimpleHTTPServer 0
COMMAND ${python_command} -m SimpleHTTPServer ${port}
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/html
USES_TERMINAL)
add_dependencies(docpreview html)