token = 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

.PHONY: all pkg_local pkg_push container container_push

all:
	/bin/true

pkg_local:
	mv ./cache /tmp
	VERBOSE=1 FORCE=1 UPLOAD=local docker compose -f ../pkgtools/docker-compose.build.yml run build
	mv /tmp/cache .

pkg_push:
	mv ./cache /tmp
	VERBOSE=1 FORCE=1 UPLOAD=scp docker compose -f ../pkgtools/docker-compose.build.yml run build
	mv /tmp/cache .

container:
# Building the container requires building some libraries with CUDA/GPU
# There are several ways to accomplish this:
# https://stackoverflow.com/questions/59691207/docker-build-with-nvidia-runtime
#
# OPTION 1 (build using legacy builder)
# This requires adding the GPU to the default builder as described here: https://docs.nvidia.com/datacenter/cloud-native/container-toolkit/latest/install-guide.html
# This will modify /etc/docker/daemon.json and then add this line to the dockerfile:
# RUN cd /usr/local/src/comfyui/custom_nodes/ComfyUI-Hunyuan3DWrapper/hy3dgen/texgen/custom_rasterizer && python setup.py install
# Then build with this command:
# DOCKER_BUILDKIT=0 docker build --tag comfy:latest . < ./Dockerfile
#
# OPTION 2 (build using buildx/buildkit)
# This requires using a buildx builder with GPU driver support
# Create the builder:
# docker buildx create --name gpubuilder --driver-opt image=crazymax/buildkit:v0.25.0-ubuntu-nvidia --bootstrap
# Add these lines to the dockerfile:
# syntax=docker/dockerfile:1.14.0-labs
# RUN --device=nvidia.com/gpu cd /usr/local/src/comfyui/custom_nodes/ComfyUI-Hunyuan3DWrapper/hy3dgen/texgen/custom_rasterizer && python setup.py install
# Then build with this command:
# docker buildx --builder gpubuilder build --load --tag comfy:latest . < ./Dockerfile
#
# OPTION 3 (build using the image and commit changes back)
# This option it to build everything that doesnt require the GPU/CUDA
# Then use that container to build the rest and commit the changes into the final image
# Overall these seems to be the simplest approach
	cp ../certs/github/systems@reimage.ai.key ./private-repo-key
	docker build --ssh default --tag comfy:tmp . < ./Dockerfile
#	docker build --no-cache --ssh default --tag comfy:tmp . < ./Dockerfile
	docker run --gpus all -it --name tmp_gpu comfy:tmp "cd /usr/local/src/comfyui/custom_nodes/ComfyUI-Hunyuan3DWrapper/hy3dgen/texgen/custom_rasterizer && python3 setup.py install"
	docker commit tmp_gpu comfy:latest || true
	docker rm tmp_gpu

container_release_beta:
	docker tag comfy:latest reimage/comfy:beta && docker push reimage/comfy:beta 

container_release_datacenter:
	docker tag reimage/comfy:beta reimage/comfy:datacenter && docker push reimage/comfy:datacenter

container_release_cloud:
	docker tag reimage/comfy:beta reimage/comfy:cloud && docker push reimage/comfy:cloud

cache_clean:
	sudo rm -rf ./cache

# requires huggingface cli to be installed:
# curl -LsSf https://hf.co/cli/install.sh | bash
# check these with:
# cat Makefile | grep 'hf download' | awk '{print $6}' | grep safetensors | while read st ; do find src/workflows/ -type f | xargs grep -q $st || echo "$st not found"; done
# or
# find cache/ -type f -name '*.safetensors' | while read st; do st="`basename $st`" ; grep -q $st src/workflows/* || echo $st not found; done
cache_update:
	mkdir -p ./cache
	mkdir -p ./cache/models/checkpoints
	mkdir -p ./cache/models/clip
	mkdir -p ./cache/models/clip_vision
	mkdir -p ./cache/models/controlnet
	mkdir -p ./cache/models/style_models
	mkdir -p ./cache/models/diffusion_models
	mkdir -p ./cache/models/unet
	mkdir -p ./cache/models/vae
	mkdir -p ./cache/models/prompt_generator
	mkdir -p ./cache/models/model_patches
	mkdir -p ./cache/models/RMBG

# hf download --token $(token) Comfy-Org/stable-diffusion-v1-5-archive v1-5-pruned-emaonly-fp16.safetensors --local-dir ./cache/models/checkpoints
# hf download --token $(token) stable-diffusion-v1-5/stable-diffusion-v1-5 v1-5-pruned-emaonly.safetensors --local-dir ./cache/models/checkpoints
# hf download --token $(token) XLabs-AI/flux-controlnet-depth-v3 flux-depth-controlnet-v3.safetensors --local-dir ./cache/models/controlnet
# hf download --token $(token) black-forest-labs/FLUX.1-Kontext-dev flux1-kontext-dev.safetensors --local-dir ./cache/models/unet
# hf download --token $(token) black-forest-labs/FLUX.1-Fill-dev flux1-fill-dev.safetensors --local-dir ./cache/models/unet
# hf download --token $(token) 6chan/flux1-kontext-dev-fp8 flux1-kontext-dev-fp8-e4m3fn.safetensors --local-dir ./cache/models/diffusion_models
	hf download --token $(token) XLabs-AI/flux-controlnet-hed-v3 flux-hed-controlnet-v3.safetensors --local-dir ./cache/models/controlnet
	hf download --token $(token) XLabs-AI/flux-controlnet-depth-v3 flux-depth-controlnet-v3.safetensors --local-dir ./cache/models/controlnet
	hf download --token $(token) XLabs-AI/flux-controlnet-canny-v3 flux-canny-controlnet-v3.safetensors --local-dir ./cache/models/controlnet
	hf download --token $(token) lllyasviel/Annotators ControlNetHED.pth --local-dir ./cache/models/controlnet
	hf download --token $(token) comfyanonymous/flux_text_encoders t5xxl_fp16.safetensors --local-dir ./cache/models/clip
	hf download --token $(token) comfyanonymous/flux_text_encoders clip_l.safetensors --local-dir ./cache/models/clip
	hf download --token $(token) black-forest-labs/FLUX.1-Redux-dev flux1-redux-dev.safetensors --local-dir ./cache/models/style_models
	hf download --token $(token) black-forest-labs/FLUX.1-dev flux1-dev.safetensors --local-dir ./cache/models/unet
	hf download --token $(token) yichengup/flux.1-fill-dev-OneReward unet_bf16.safetensors --local-dir ./cache/models/unet
	ln -sf ./unet_bf16.safetensors ./cache/models/unet/flux-fill-onereward.safetensors
	hf download --token $(token) yichengup/flux.1-fill-dev-OneReward unet_fp8.safetensors --local-dir ./cache/models/unet
	ln -sf ./unet_fp8.safetensors ./cache/models/unet/flux-fill-onereward-fp8.safetensors
	hf download --token $(token) black-forest-labs/FLUX.1-dev ae.safetensors --local-dir ./cache/models/vae
	hf download --token $(token) Comfy-Org/sigclip_vision_384 sigclip_vision_patch14_384.safetensors --local-dir ./cache/models/clip_vision
	hf download --token $(token) comfyanonymous/flux_text_encoders t5xxl_fp8_e4m3fn_scaled.safetensors --local-dir ./cache/models/text_encoders
	hf download --token $(token) comfyanonymous/flux_text_encoders t5xxl_fp8_e4m3fn.safetensors --local-dir ./cache/models/text_encoders
	hf download --token $(token) tencent/Hunyuan3D-2 hunyuan3d-dit-v2-0/model.fp16.safetensors --local-dir ./cache/models/diffusion_models
	ln -sf hunyuan3d-dit-v2-0/model.fp16.safetensors ./cache/models/diffusion_models/hunyuan3d-dit-v2-0-fp16.safetensors
	hf download --token $(token) Comfy-Org/Qwen-Image_ComfyUI split_files/text_encoders/qwen_2.5_vl_7b_fp8_scaled.safetensors --local-dir ./cache/models/text_encoders
	ln -sf ./split_files/text_encoders/qwen_2.5_vl_7b_fp8_scaled.safetensors ./cache/models/text_encoders/qwen_2.5_vl_7b_fp8_scaled.safetensors
	hf download --token $(token) Comfy-Org/Qwen-Image_ComfyUI split_files/vae/qwen_image_vae.safetensors --local-dir ./cache/models/vae
	ln -sf ./split_files/vae/qwen_image_vae.safetensors ./cache/models/vae/qwen_image_vae.safetensors
	hf download --token $(token) Comfy-Org/Qwen-Image-Edit_ComfyUI split_files/diffusion_models/qwen_image_edit_fp8_e4m3fn.safetensors --local-dir ./cache/models/diffusion_models
	ln -sf ./split_files/diffusion_models/qwen_image_edit_fp8_e4m3fn.safetensors ./cache/models/diffusion_models/qwen_image_edit_fp8_e4m3fn.safetensors
	hf download --token $(token) Comfy-Org/Qwen-Image-Edit_ComfyUI split_files/diffusion_models/qwen_image_edit_fp8_e4m3fn.safetensors --local-dir ./cache/models/diffusion_models
	ln -sf ./split_files/diffusion_models/qwen_image_edit_fp8_e4m3fn.safetensors ./cache/models/diffusion_models/qwen_image_edit_fp8_e4m3fn.safetensors
	hf download --token $(token) Comfy-Org/Qwen-Image_ComfyUI split_files/diffusion_models/qwen_image_fp8_e4m3fn.safetensors --local-dir ./cache/models/diffusion_models
	ln -sf ./split_files/diffusion_models/qwen_image_fp8_e4m3fn.safetensors ./cache/models/diffusion_models/qwen_image_fp8_e4m3fn.safetensors
	hf download --token $(token) valiantcat/Qwen-Image-Edit-MeiTu qwen_image_edit_meitu_fp8_e4m3fn.safetensors --local-dir ./cache/models/diffusion_models
	hf download --token $(token) lightx2v/Qwen-Image-Lightning Qwen-Image-Lightning-4steps-V1.0.safetensors --local-dir ./cache/models/loras/
	hf download --token $(token) ali-vilab/ACE_Plus subject/comfyui_subject_lora16.safetensors --local-dir ./cache/models/loras/
	ln -sf ./subject/comfyui_subject_lora16.safetensors ./cache/models/loras/comfyui_subject_lora16.safetensors
	hf download --token $(token) lightx2v/Qwen-Image-Lightning Qwen-Image-Edit-Lightning-8steps-V1.0-bf16.safetensors --local-dir ./cache/models/loras/
	hf download --token $(token) lightx2v/Qwen-Image-Lightning Qwen-Image-Edit-Lightning-4steps-V1.0-bf16.safetensors --local-dir ./cache/models/loras/
	hf download --token $(token) Comfy-Org/Qwen-Image-DiffSynth-ControlNets split_files/model_patches/qwen_image_canny_diffsynth_controlnet.safetensors --local-dir ./cache/models/model_patches
	ln -sf ./split_files/model_patches/qwen_image_canny_diffsynth_controlnet.safetensors ./cache/models/model_patches/qwen_image_canny_diffsynth_controlnet.safetensors
	hf download --token $(token) Comfy-Org/Qwen-Image-DiffSynth-ControlNets split_files/model_patches/qwen_image_depth_diffsynth_controlnet.safetensors --local-dir ./cache/models/model_patches
	ln -sf ./split_files/model_patches/qwen_image_depth_diffsynth_controlnet.safetensors ./cache/models/model_patches/qwen_image_depth_diffsynth_controlnet.safetensors
	hf download --token $(token) Comfy-Org/Qwen-Image-DiffSynth-ControlNets split_files/model_patches/qwen_image_inpaint_diffsynth_controlnet.safetensors --local-dir ./cache/models/model_patches
	ln -sf ./split_files/model_patches/qwen_image_inpaint_diffsynth_controlnet.safetensors ./cache/models/model_patches/qwen_image_inpaint_diffsynth_controlnet.safetensors
	hf download --token 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 Qwen/Qwen2.5-VL-3B-Instruct --local-dir ./cache/models/prompt_generator/Qwen2.5-VL-3B-Instruct
#	mkdir -p ./cache/models/loras/brava
#	mkdir -p ./cache/models/loras/owens_corning
#	hf download --token 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 reimager/flux_lora_roof aged_cedar_shake-cloudy.safetensors --local-dir ./cache/models/loras/brava/
#	hf download --token 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 reimager/flux_lora_roof aged_cedar_shake-full_sun.safetensors --local-dir ./cache/models/loras/brava/
#	hf download --token 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 reimager/owen-lora --local-dir ./cache/models/loras/owens_corning/
	hf download --token $(token) ali-vilab/ACE_Plus subject/comfyui_subject_lora16.safetensors --local-dir ./cache/models/loras/
	hf download --token $(token) reimage-ai/comfy-service-assets --repo-type dataset --local-dir ./cache/assets
	hf download --token $(token) reimage-ai/put-it-here qwen-edit-put_it_here_v2.safetensors --local-dir  ./cache/models/loras/
	hf download --token $(token) reimage-ai/lora_staging  qwen-unstaging.safetensors --local-dir  ./cache/models/loras/
	mkdir -p ./cache/models/deeplabv3
	hf download --token $(token) reimage-ai/roof-segmentor  finetuned_deeplabv3_resnet50_epoch_30.pt --local-dir  ./cache/models/deeplabv3/
	mkdir -p ./cache/models/sam3
	hf download --token $(token) facebook/sam3 sam3.pt --local-dir ./cache/models/sam3/
	hf download --token $(token) aidiffuser/Qwen-Image-Edit-2509 Qwen-Image-Edit-2509_fp8_e4m3fn.safetensors --local-dir ./cache/models/diffusion_models/
	hf download --token $(token) reimage-ai/lora_interior qwen-edit-interior-v1.safetensors --local-dir ./cache/models/loras/
	hf download --token $(token) tarn59/apply_texture_qwen_image_edit_2509 apply_texture_v2_qwen_image_edit_2509.safetensors --local-dir ./cache/models/loras/
	hf download --token $(token) InstantX/Qwen-Image-ControlNet-Union diffusion_pytorch_model.safetensors  --local-dir ./cache/models/controlnet/Qwen-Image-ControlNet-Union
	ln -sf ./Qwen-Image-ControlNet-Union/diffusion_pytorch_model.safetensors ./cache/models/controlnet/Qwen-Image-InstantX-ControlNet-Union.safetensors
	hf download --token $(token) InstantX/Qwen-Image-ControlNet-Inpainting diffusion_pytorch_model.safetensors  --local-dir ./cache/models/controlnet/Qwen-Image-ControlNet-Inpainting
	ln -sf ./Qwen-Image-ControlNet-Inpainting/diffusion_pytorch_model.safetensors ./cache/models/controlnet/Qwen-Image-InstantX-ControlNet-Inpainting.safetensors
	hf download --token $(token) InstantX/FLUX.1-dev-Controlnet-Union diffusion_pytorch_model.safetensors --local-dir ./cache/models/controlnet/FLUX.1-dev-Controlnet-Union
	ln -sf ./FLUX.1-dev-Controlnet-Union/diffusion_pytorch_model.safetensors ./cache/models/controlnet/Flux1-dev-Controlnet-Union.safetensors
	hf download --token $(token) Comfy-Org/flux2-dev split_files/vae/flux2-vae.safetensors --local-dir ./cache/models/vae
	ln -sf ./split_files/vae/flux2-vae.safetensors ./cache/models/vae/flux2-vae.safetensors
	hf download --token $(token) Comfy-Org/flux2-dev split_files/text_encoders/mistral_3_small_flux2_fp8.safetensors --local-dir ./cache/models/text_encoders
	ln -sf ./split_files/text_encoders/mistral_3_small_flux2_fp8.safetensors ./cache/models/text_encoders/mistral_3_small_flux2_fp8.safetensors
	hf download --token $(token) orabazes/FLUX.2-dev-GGUF flux2_dev_Q6_K.gguf --local-dir ./cache/models/unet
	hf download --token $(token) reimage-ai/lora_roof_flux2 lora_roof_flux2.safetensors --local-dir ./cache/models/loras/
	find ./cache/ -type d -name '.cache' | xargs rm -rf
	sudo chmod -R 777 ./cache
	sudo find ./cache -exec touch -m -d '1/1/2000' {} \; 2>/dev/null

cache: cache_update

