diff --git a/tools/qmake/mkspecs/features/functions.prf b/tools/qmake/mkspecs/features/functions.prf
index cab79ff28774c3a0893bfddd4b7fe7d37ffb21eb..40532f709be1fe9ee1cc71cfb556f8b04f72ed3f 100644
--- a/tools/qmake/mkspecs/features/functions.prf
+++ b/tools/qmake/mkspecs/features/functions.prf
@@ -167,8 +167,12 @@ defineReplace(findOrBuildNinja) {
         log("Build directory is different from source directory - copying ninja sources to the build tree...")
         shadow_3rd_party_path = $$system_path($$shadowed($$src_3rd_party_dir))
 
-        !exists($$dirname(out)): mkpath($$shadow_3rd_party_path)
-        system("$$QMAKE_COPY_DIR $$system_quote($$system_path($$absolute_path("ninja", "$$src_3rd_party_dir"))) $$system_quote($$shadow_3rd_party_path)")
+        !exists($$dirname(out)): mkpath($$dirname(out))
+        copy_dir_files {
+          system("$$QMAKE_COPY_DIR $$system_quote($$system_path($$absolute_path("ninja", "$$src_3rd_party_dir"))) $$system_quote($$dirname(out))")
+        } else {
+          system("$$QMAKE_COPY_DIR $$system_quote($$system_path($$absolute_path("ninja", "$$src_3rd_party_dir"))) $$system_quote($$shadow_3rd_party_path)")
+        }
       }
       system("cd $$system_quote($$dirname(out)) && python configure.py --bootstrap")
     }