csukuangfj / onnxruntime-libs

onnxruntime pre-compiled libs
81 stars 21 forks source link

Debug build for x64 windows #20

Closed L3b1n closed 3 months ago

L3b1n commented 3 months ago

Hi there!

Could you provide some info on how to build debug version for windows x64? I try to set this flags: CMAKE_BUILD_TYPE="Debug" CMAKE_CONFIGURATION_TYPES="Debug" CMAKE_OPTIONS="$CMAKE_OPTIONS -DCMAKE_MSVC_RUNTIME_LIBRARY='MultiThreaded$<$:Debug>'" CMAKE_OPTIONS="$CMAKE_OPTIONS -DONNXRUNTIME_USE_STATIC_MSVCRT=ON" CMAKE_OPTIONS="$CMAKE_OPTIONS -Dprotobuf_MSVC_STATIC_RUNTIME=ON" CMAKE_OPTIONS="$CMAKE_OPTIONS -Dgtest_force_shared_crt=OFF" CMAKE_OPTIONS="$CMAKE_OPTIONS -A x64" CMAKE_OPTIONS="$CMAKE_OPTIONS -Donnxruntime_BUILD_UNIT_TESTS=OFF" CMAKE_OPTIONS="$CMAKE_OPTIONS -DONNX_CUSTOM_PROTOC_EXECUTABLE=$GITHUB_WORKSPACE/bin/protoc" CMAKE_OPTIONS="$CMAKE_OPTIONS -DABSL_PROPAGATE_CXX_STD=ON"

But, unfortunately I get errors while compiling onnxruntime_providers.vcxproj: libprotobuf-lited.lib(message_lite.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MTd_StaticDebug' doesn't match value 'MT_StaticRelease' in dllmain.obj

I would really appreciate any help.

csukuangfj commented 3 months ago

Please see https://github.com/csukuangfj/onnxruntime-build/actions/runs/9184634501/workflow

L3b1n commented 3 months ago

I have tried this out and get the same link error: libprotobuf-lited.lib(message_lite.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MTd_StaticDebug' doesn't match value 'MT_StaticRelease' in dllmain.obj

For my needs I remove OnnxRuntime update. I need to have windows 7 support, so I add some fixes into source code. Changes are relevant, because release version builds without any issues.

csukuangfj commented 3 months ago

Please attach all of the logs in a file and please also post the complete commands you are using.

csukuangfj commented 3 months ago

note the commands should be suitable for people to copy and paste into the terminal to reproduce it.

L3b1n commented 3 months ago

I use following script (build-static_lib.sh):

#!/usr/bin/env bash

set -ex

CMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE:=Debug}
SOURCE_DIR=${SOURCE_DIR:=static_lib}
BUILD_DIR=${BUILD_DIR:=build-${CMAKE_BUILD_TYPE}/static_lib}
OUTPUT_DIR=${OUTPUT_DIR:=output-${CMAKE_BUILD_TYPE}/static_lib}
ONNXRUNTIME_SOURCE_DIR=${ONNXRUNTIME_SOURCE_DIR:=onnxruntime}
ONNXRUNTIME_VERSION=${ONNXRUNTIME_VERSION:=$(cat ONNXRUNTIME_VERSION)}

# Add runtime library options for specific libraries
CMAKE_OPTIONS="$CMAKE_OPTIONS -DCMAKE_MSVC_RUNTIME_LIBRARY='MultiThreaded$<$<CONFIG:Debug>:Debug>'"
CMAKE_OPTIONS="$CMAKE_OPTIONS -DONNXRUNTIME_USE_STATIC_MSVCRT=ON"
CMAKE_OPTIONS="$CMAKE_OPTIONS -Dprotobuf_MSVC_STATIC_RUNTIME=ON"
CMAKE_OPTIONS="$CMAKE_OPTIONS -Dgtest_force_shared_crt=OFF"
CMAKE_OPTIONS="$CMAKE_OPTIONS -A x64"
CMAKE_OPTIONS="$CMAKE_OPTIONS -Donnxruntime_BUILD_UNIT_TESTS=OFF"
CMAKE_OPTIONS="$CMAKE_OPTIONS -DABSL_PROPAGATE_CXX_STD=ON"

CMAKE_BUILD_OPTIONS=$CMAKE_BUILD_OPTIONS

echo "CMAKE_BUILD_TYPE: $CMAKE_BUILD_TYPE"
echo "CMAKE_BUILD_OPTIONS: $CMAKE_BUILD_OPTIONS"

case $(uname -s) in
Darwin) CPU_COUNT=$(sysctl -n hw.physicalcpu) ;;
Linux) CPU_COUNT=$(grep ^cpu\\scores /proc/cpuinfo | uniq | awk '{print $4}') ;;
*) CPU_COUNT=$NUMBER_OF_PROCESSORS ;;
esac
PARALLEL_JOB_COUNT=${PARALLEL_JOB_COUNT:=$CPU_COUNT}

cd $(dirname $0)
echo "pwd: $PWD"

echo "SOURCE_DIR: $SOURCE_DIR"
echo "BUILD_DIR: $BUILD_DIR"
echo "OUTPUT_DIR: $OUTPUT_DIR"
echo "ONNXRUNTIME_SOURCE_DIR: $ONNXRUNTIME_SOURCE_DIR"
echo "ONNXRUNTIME_VERSION: $ONNXRUNTIME_VERSION"
echo "CMAKE_OPTIONS: $CMAKE_OPTIONS"
echo "CMAKE_BUILD_OPTIONS: $CMAKE_BUILD_OPTIONS"
echo "PARALLEL_JOB_COUNT: $PARALLEL_JOB_COUNT"

cmake \
    -S $SOURCE_DIR \
    -B $BUILD_DIR \
    -D CMAKE_BUILD_TYPE=$CMAKE_BUILD_TYPE \
    -D CMAKE_CONFIGURATION_TYPES=$CMAKE_BUILD_TYPE \
    -D CMAKE_INSTALL_PREFIX=$OUTPUT_DIR \
    -D ONNXRUNTIME_SOURCE_DIR=$(pwd)/$ONNXRUNTIME_SOURCE_DIR \
    --compile-no-warning-as-error \
    $CMAKE_OPTIONS

read -p "Press enter to continue"

cmake \
    --build $BUILD_DIR \
    --config $CMAKE_BUILD_TYPE \
    --parallel $PARALLEL_JOB_COUNT \
    $CMAKE_BUILD_OPTIONS
cmake --install $BUILD_DIR --config $CMAKE_BUILD_TYPE

read -p "Press enter to continue"

I run script in the git bash by command ./build-static_lib.sh.

Here you can find example of log file. Error is generated always in the same project. log1.txt

csukuangfj commented 3 months ago

: error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MTd_StaticDebug' doesn't match value 'MT_StaticRelease'

Is this your first time building onnxruntime?

Looks to me that you have mixed Debug and Release build.

Could you remove the build directory and retry?

L3b1n commented 3 months ago

Is this your first time building onnxruntime?

I have successfully built release mode static lib using same script, but with the CMAKE_BUILD_TYPE=Release set up. Also I set build directory to fit Release or Debug separately.

Looks to me that you have mixed Debug and Release build.

But I set CMAKE_BUILD_TYPE=Debug at the top of the script to avoid this mismatch.

Could you remove the build directory and retry?

I have already tried this, nothing have changed.

csukuangfj commented 3 months ago

In that case, I suggest that you use the static debug lib provided by us.

L3b1n commented 3 months ago

I managed to get to the bottom of the issue. I realised that my static_lib/CMakeLists.txt file was missing those lines:

if(NOT BUILD_SHARED_LIBS AND MSVC)
    # see https://cmake.org/cmake/help/latest/prop_tgt/MSVC_RUNTIME_LIBRARY.html
    # https://stackoverflow.com/questions/14172856/compile-with-mt-instead-of-md-using-cmake
    if(MSVC)
        add_compile_options(
            $<$<CONFIG:>:/MT> #---------|
            $<$<CONFIG:Debug>:/MTd> #---|-- Statically link the runtime libraries
            $<$<CONFIG:Release>:/MT> #--|
        )
    endif()
endif()

Once I added these lines, I was able to successfully build the debug version. @csukuangfj thx for help!

csukuangfj commented 3 months ago

Great!

I thought you were using https://github.com/csukuangfj/onnxruntime-build/blob/main/static_lib/CMakeLists.txt#L13 without modifications.