'why does only running cmake -S. -Bbuild twice works?

Im using llvm in my c++ project that I build using cmake. when I run this command cmake -S. -Bbuild for some reason findpackage() doesnt find llvm and I get this output

Could not find a package configuration file provided by "LLVM" with any of
the following names:

LLVMConfig.cmake
llvm-config.cmake

Add the installation prefix of "LLVM" to CMAKE_PREFIX_PATH or set
"LLVM_DIR" to a directory containing one of the above files.  If "LLVM"
provides a separate development package or SDK, be sure it has been
installed.

However, when I run cmake -S. -Bbuild again immediatly after, everything works fine !

-- Found ZLIB: /usr/lib/x86_64-linux-gnu/libz.so (found version "1.2.11") 
-- Found LibXml2: /usr/lib/x86_64-linux-gnu/libxml2.so (found version "2.9.10") 
-- Found LLVM 13.0.0
-- Using LLVMConfig.cmake in: /home/abde_hn/llvm-project/build/lib/cmake/llvm
-- Found Z3 
-- Using Z3Config.cmake in: $Z3_DIR}
-- Found Doxygen: /usr/bin/doxygen (found version "1.8.17") found components: doxygen dot 
Doxygen build started
-- Configuring done
-- Generating done
-- Build files have been written to: /home/abde_hn/del-lifter/build

My CMakeLists.txt looks like this:

cmake_minimum_required(VERSION 3.13.4)
SET(CMAKE_INSTALL_PREFIX "/home/abde_hn/llvm-project/build;/home/abde_hn/z3/build")
project(${This}C CXX)
find_package(LLVM REQUIRED CONFIG)
message(STATUS "Found LLVM ${LLVM_PACKAGE_VERSION}")
message(STATUS "Using LLVMConfig.cmake in: ${LLVM_DIR}")
find_package(Z3 REQUIRED CONFIG)
message(STATUS "Found Z3 ${Z3_PACKAGE_VERSION}")
message(STATUS "Using Z3Config.cmake in: $Z3_DIR}")
include_directories(${LLVM_INCLUDE_DIRS})
include_directories(${Z3_C_INCLUDE_DIRS})
include_directories(${Z3_INCLUDE_DIRS})
include_directories("/home/abde_hn/graphviz/build/include")
add_definitions(${LLVM_DEFINITIONS})
set(This DEL)
set(CMAKE_C_STANDARD 99)
set(CMAKE_CXX_STANDARD 17)
set(CXXFLAGS += `llvm-config --cxxflags --ldflags --libs core`)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
enable_testing()
set(Headers BB.h Func.h Reg.h Immediate.h Instr.h Utilities.h OpCode.h ll.h ll_api.h ll_z.h 
ll_api_z.h Graph.h tabulate.h)
set(Sources main.cpp BB.cpp Func.cpp Reg.cpp Immediate.cpp Instr.cpp Utilities.cpp OpCode.cpp 
ll.cpp ll_api.cpp ll_z.cpp ll_api_z.cpp Graph.cpp)
set(Z3_LIBRARIES "/home/abde_hn/z3/build/lib/libz3.so")
set(graphiz_LIBRARIES 
"/home/abde_hn/graphviz/build/lib/libgvc.so;/home/abde_hn/graphviz/build/lib/libcgraph.so")
add_executable(${This} ${Sources})
llvm_map_components_to_libnames(llvm_libs support core irreader)
target_link_libraries(${This} ${llvm_libs} ${Z3_LIBRARIES} ${graphiz_LIBRARIES})


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source