@@ -325,36 +325,82 @@ else() # macOS and Linux
325325 endif ()
326326
327327 else () # Linux
328- # Instructions took from https://github.com/microsoft/onnxruntime/discussions/6489
329- # Step 1: Download the file
330- file (DOWNLOAD
331- https://github.com/microsoft/onnxruntime/releases/download/v1.22.0/onnxruntime-linux-x64-1.22.0.tgz
332- ${CMAKE_BINARY_DIR} /onnxruntime-linux-x64-1.22.0.tgz
333- SHOW_PROGRESS
334- )
335-
336- # Step 2: Extract the archive
337- execute_process (
338- COMMAND tar -xzf onnxruntime-linux-x64-1.22.0.tgz
339- WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
340- )
328+ # ONNX RUNTIME LINUX CONFIG
329+ # NOTE: users can specify their own ONNX_ROOT_PATH (this is the base folder for ONNX) on the command line when evoking cmake.
330+ # this can be done via -DONNX_ROOT_PATH=/path/to/onnx
331+
332+ # If the path isn't provided, it'll automatically download the 1.23.0 release to the build folder,
333+ # extract it, and then link it up
334+
335+ # Set the ONNXRUNTIME version and arch, allows for quick updates
336+ set (ONNXRUNTIME_VERSION "1.23.0" )
337+ set (ONNXRUNTIME_ARCH "x64" )
338+ set (ONNXRUNTIME_DIR_NAME "onnxruntime-linux-${ONNXRUNTIME_ARCH} -${ONNXRUNTIME_VERSION} " )
339+ set (ONNXRUNTIME_TGZ_NAME "${ONNXRUNTIME_DIR_NAME} .tgz" )
340+ # build up the URL based on the version and name
341+ set (ONNXRUNTIME_URL "https://github.com/microsoft/onnxruntime/releases/download/v${ONNXRUNTIME_VERSION} /${ONNXRUNTIME_TGZ_NAME} " )
342+
343+ # Cache variable is editable by users *and* is properly stored
344+ set (ONNX_ROOT_PATH "" CACHE PATH "Path to the root of a pre-installed ONNX Runtime (e.g., /path/to/onnxruntime-linux-x64-1.23.0)" )
345+
346+ # Download the onnxruntime if it doesn't exist or if the path is bad
347+ if (NOT ONNX_ROOT_PATH OR NOT EXISTS "${ONNX_ROOT_PATH} /include/onnxruntime_cxx_api.h" )
348+ set (EXTRACTED_ONNX_PATH "${CMAKE_BINARY_DIR} /${ONNXRUNTIME_DIR_NAME} " )
349+
350+ if (NOT IS_DIRECTORY ${EXTRACTED_ONNX_PATH} )
351+ message (STATUS "ONNX_ROOT_PATH not found. Downloading ONNX Runtime v${ONNXRUNTIME_VERSION} ..." )
352+
353+ # logic for downloading...
354+ set (DOWNLOAD_LOCATION "${CMAKE_BINARY_DIR} /${ONNXRUNTIME_TGZ_NAME} " )
355+ file (DOWNLOAD
356+ ${ONNXRUNTIME_URL}
357+ ${DOWNLOAD_LOCATION}
358+ SHOW_PROGRESS
359+ STATUS download_status
360+ )
361+ list (GET download_status 0 error_code)
362+ if (NOT error_code EQUAL 0)
363+ list (GET download_status 1 error_message)
364+ message (FATAL_ERROR "Failed to download ONNX Runtime: ${error_message} " )
365+ endif ()
366+
367+ # logic for extracting the tarball to our working directory
368+ message (STATUS "Extracting ONNX Runtime..." )
369+ execute_process (
370+ COMMAND ${CMAKE_COMMAND} -E tar xzf "${DOWNLOAD_LOCATION} "
371+ WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
372+ )
373+ endif ()
374+
375+ # Update the ONNX_ROOT_PATH to point to our downloaded version, force ensures we update it
376+ set (ONNX_ROOT_PATH "${EXTRACTED_ONNX_PATH} " CACHE PATH "Path to the downloaded ONNX Runtime" FORCE)
377+ message (STATUS "Using downloaded ONNX Runtime at: ${ONNX_ROOT_PATH} " )
378+ else ()
379+ message (STATUS "Using user-provided ONNX Runtime at: ${ONNX_ROOT_PATH} " )
380+ endif ()
341381
342- # Step 3: Copy files to /usr/local
343- execute_process (
344- COMMAND sudo cp -r ${CMAKE_BINARY_DIR} /onnxruntime-linux-x64-1.22.0/include /usr/local/
345- )
346- execute_process (
347- COMMAND sudo cp -r ${CMAKE_BINARY_DIR} /onnxruntime-linux-x64-1.22.0/lib /usr/local/
382+ # configure and find headers from the onnxruntime
383+ find_path (ONNX_INCLUDE_DIRS
384+ NAMES onnxruntime_cxx_api.h
385+ HINTS "${ONNX_ROOT_PATH} /include"
386+ REQUIRED
348387 )
349388
350- # Step 4: Run ldconfig
351- execute_process (
352- COMMAND sudo ldconfig
353- WORKING_DIRECTORY /usr/local/lib
389+ find_library (ONNX_RUNTIME_LIB
390+ NAMES onnxruntime
391+ HINTS " ${ONNX_ROOT_PATH} /lib"
392+ REQUIRED
354393 )
355394
356- # Link against ONNX Runtime
357- target_link_libraries (SerialProgramsLib PRIVATE onnxruntime)
395+ if (ONNX_INCLUDE_DIRS AND ONNX_RUNTIME_LIB)
396+ # only link if it was found, otherwise we error out
397+ message (STATUS "Found ONNX Include Dirs: ${ONNX_INCLUDE_DIRS} " )
398+ message (STATUS "Found ONNX Library: ${ONNX_RUNTIME_LIB} " )
399+ target_include_directories (SerialProgramsLib PUBLIC "${ONNX_INCLUDE_DIRS} " )
400+ target_link_libraries (SerialProgramsLib PUBLIC "${ONNX_RUNTIME_LIB} " )
401+ else ()
402+ message (FATAL_ERROR "Could not find ONNX Runtime headers or library." )
403+ endif ()
358404 # Find OpenCV
359405 find_package (OpenCV REQUIRED HINTS "/usr/local/opt/opencv/lib/cmake/opencv4/" )
360406 endif ()
0 commit comments