delayload inconsistency between Visual Studio and Ninja generators

Consider the following example which has a delay loaded dll. When building with the Visual Studio generator it builds fine. However, with the Ninja generator you get:

dll.lib(dll.dll) : error LNK2001: unresolved external symbol __delayLoadHelper2
code.dll : fatal error LNK1120: 1 unresolved externals                                                                                                           
b:code.lib /pdb:code.pdb /dll /version:0.0 /machine:x64 /IN
ninja: build stopped: subcommand failed. 

If you manually add delayimp as a linked library, it works with Ninja. Of course, this is an easy workaround but ideally it wouldn’t be necessary. If you look at the vcxproj from the Visual Studio generator delayimp isn’t added to to linked library. Does anyone have insight into this behavior difference? Should it be considered a bug?

CMakeLists.txt

cmake_minimum_required(VERSION 3.16)
project(delayimp_test)
add_library(dll SHARED dll.cpp)
target_compile_definitions(dll PRIVATE DLL_EXPORTS)
add_library(code SHARED code.cpp)
target_link_libraries(code PRIVATE dll)
target_link_options(code PRIVATE "/DELAYLOAD:dll.dll")

code.cpp

#include "dll.hpp"

int zazz(){
    return do_something();
}

dll.hpp

#pragma once

#ifdef DLL_EXPORTS
#define DLL_API __declspec(dllexport)
#else
#define DLL_API __declspec(dllimport)
#endif

DLL_API int do_something();

dll.cpp

#include "dll.hpp"

int do_something(){
    return 20;
}

I suspect that Visual Studio is seeing the /delayload flag and just Making It Work™ for you. I would suggest adding delayimp.lib to your target_link_libraries manually.

Ok - thanks for your thoughts.